Skip to content Skip to sidebar Skip to footer

Place 3 Buttons In A Linearlayout To Occupy Equal Amount Of Space

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 xml :

Solution 1:

The following layout should work. weights are for LinearLayouts..

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:weightSum="3"
><Buttonandroid:id="@+id/button1"...android:layout_weight="1"/><Buttonandroid:id="@+id/button2"...android:layout_weight="1"/><Buttonandroid:id="@+id/button3"...android:layout_weight="1"/></LinearLayout>

Solution 2:

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

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"
><Buttonandroid: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"
/><Buttonandroid: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"
/><Buttonandroid: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>

Solution 3:

Divide the weight sum to equal proportion in all buttons.

Remove layout_gravity property and add android:layout_weight=0.33.

It will work

Solution 4:

Check out this:

<RelativeLayoutandroid:id="@+id/layout"android:layout_width="fill_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/button1"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:layout_alignParentLeft="true"/><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"><Buttonandroid:id="@+id/button2"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:layout_toRightOf="@+id/button1"/></RelativeLayout><Buttonandroid:id="@+id/button3"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:layout_alignParentRight="true"/></RelativeLayout>

Solution 5:

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

Post a Comment for "Place 3 Buttons In A Linearlayout To Occupy Equal Amount Of Space"