It is not possible to align one image button on the right and the other on the left

Is this xml in which I am trying to save one button of an image right and left? I do not understand why this is not happening, although I also installed grvaity?

<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_gravity="fill"> <ImageButton android:layout_width="wrap_content" android:layout_gravity="left" android:id="@+id/imageButton1" android:src="@drawable/arrow_button_left" android:layout_height="wrap_content"></ImageButton> <ImageButton android:layout_width="wrap_content" android:layout_gravity="right" android:id="@+id/imageButton2" android:src="@drawable/arrow_button_right" android:layout_height="wrap_content"></ImageButton> </LinearLayout> 
+4
source share
3 answers

You should use RelativeLayout instead of LinearLayout and set the layout_alignParentRight attribute for the second ImageButton to true :

 android:layout_alignParentRight="true" 

so that your layout looks like this:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_gravity="fill"> <ImageButton android:layout_width="wrap_content" android:id="@+id/imageButton1" android:src="@drawable/arrow_button_left" android:layout_height="wrap_content" /> <ImageButton android:layout_width="wrap_content" android:id="@+id/imageButton2" android:src="@drawable/arrow_button_right" android:layout_height="wrap_content" android:layout_alignParentRight="true" /> </RelativeLayout> 
+18
source

You set the width of the LinearLayout to wrap_content , so it has a minimum width to place buttons inside you. It is not possible to align this button just because they do not have room for alignment. Try setting fill_parent .

+1
source

You may have forgotten android: orientation = "horizontal" in LinearLayout if you used width for wrap_content.

Else Relativle layout as above is perfect for you.

0
source

All Articles