Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

<LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     android:weightSum="1.0"     >      <Button android:id="@+id/button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/Bg"             android:background="@drawable/button_red"             android:layout_weight=".2"             android:layout_gravity="left"     />     <Button android:id="@+id/button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/Bm"             android:background="@drawable/button_red"             android:layout_weight=".2"             android:textColor="#ffffff"             android:layout_gravity="center"     />      <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@drawable/button_red"         android:layout_weight=".2"         android:text="@string/Bf"         android:layout_gravity="right"     />  </LinearLayout> 

if someone see whats wrong, thanks.

like image 570
user1527152 Avatar asked Jul 26 '12 12:07

user1527152


People also ask

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

I suggest you use LinearLayout's weightSum attribute. Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.

How do you put a space between two buttons in XML?

Use android:layout_marginTop="10.0dip" on second button.

How do you put two buttons on the same line in linear layout?

If you are placing the buttons inside the LinearLayout, give the Orientation value as "Vertical", it will automatically place the buttons in the same line. If you are using RelativeLayout, then for one button use android:layout_toLeftOf OR android:layout_toRightOf and give value as ID of other button.

How do you put a space between buttons in linear layout?

good answer. Small improvement: use a <Space android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" > </Space> instead of a <View> to make your objective clearer in your XML.


1 Answers

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

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3" >      <Button android:id="@+id/button1"             ...             android:layout_weight="1"/>      <Button android:id="@+id/button2"             ...             android:layout_weight="1"/>      <Button         android:id="@+id/button3"         ...         android:layout_weight="1"/>  </LinearLayout> 
like image 52
Ronnie Avatar answered Sep 21 '22 21:09

Ronnie