i would like to have three buttons taking equal amount of available space horizontally in a row.
I used android:layout_gravity
. What is the problem?
layout
Divide the weight sum to equal proportion in all buttons.
Remove layout_gravity property and add android:layout_weight=0.33.
It will work
Put the buttons in a relative layout. add properties to the buttons allignParentleft = true, another allignParentcenter= true and the allignParentRight = true to each button.
Check out this:
<RelativeLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button1"/>
</RelativeLayout>
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Give android:layout_weight="1"
for each Button
Here Android Developer Guide:
Layout Weight
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.
Source
besides of setting a layout_weight
you have to set layout_width
or layout_height
to 0dp
. So if you want for example to distribute the buttons horizontally , layout_width
should be 0dp and layout_weight
.2 or any number as long as is equal through the buttons you have.
so your layout should be like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bg"
android:background="@drawable/button_red"
android:layout_weight="1"
/>
<Button android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bm"
android:background="@drawable/button_red"
android:layout_weight="1"
android:textColor="#ffffff"
/>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/button_red"
android:layout_weight="1"
android:text="@string/Bf"
/>
</LinearLayout>
You can divide it like this: `
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imageButtonLikedPost">
<ImageButton
android:id="@+id/imageButtonMyPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/mypost_tab_bg" />
</RelativeLayout>
<ImageButton
android:id="@+id/imageButtonLikedPost"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/mylike_tab_bg" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageButtonLikedPost">
<ImageButton
android:id="@+id/imageButtonMyBlog"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/tab4_bg" />
</RelativeLayout>
</RelativeLayout>`