Unable to align one Image button right while the other one the left
This the xml in which I am trying to keep one image button on the right and while the left ? I dont why it is not happening though i have set the grvaity too ?
<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/arro开发者_C百科w_button_right" android:layout_height="wrap_content"></ImageButton>
</LinearLayout>
You should use RelativeLayout
instead of LinearLayout
, and set the layout_alignParentRight
attribute for the second ImageButton
to true
:
android:layout_alignParentRight="true"
so your layout would look like:
<?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>
You set LinearLayout
's width to wrap_content
so it has a minimal width to place buttons inside itself. There's no way these button can be aligned just because they have no space for alignment. Try setting fill_parent
.
may be you forget android:orientation="horizontal" in LinearLayout if you have used width to wrap_content.
Else Relativle layout as stated above is perfect for you.
精彩评论