2

i have a problem, i need to put 3 buttons in a android layout, but:

  • they should all be in one row
  • they should all be 33,3% width of the display width

i tryed some things with a table and stack layout but did not manage to get it to work with the width.

please help me

gurehbgui
  • 14,236
  • 32
  • 106
  • 178

3 Answers3

6

Use a LinearLayout as the root, then make give each button the attribute android:layout_weight="1" - which will make them all equal weights, and take up the space.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dip"
android:orientation="horizontal" >
<Button
    android:id="@+id/rvButton" 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    />
<Button
    android:id="@+id/rvButton" 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    />
<Button
    android:id="@+id/rvButton" 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    />

xdumaine
  • 10,096
  • 6
  • 62
  • 103
3

Take a Linear Layout with Horizontal orientation and put equal weight as below.

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
xdumaine
  • 10,096
  • 6
  • 62
  • 103
TNR
  • 5,839
  • 3
  • 33
  • 62
3

try this:

<LinearLayout
  android:layout_with="fill_parent"
  android:layout_height="warp_content"
  android:orientation="horizontal"
  android_weight_sum="3">
  <View 
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight=1 />
  <View 
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight=1 />
  <View 
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight=1 />
</LinearLayout>

Your LinearLayout has a sum of 3 and each View inside takes exactly one third in space of it

Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • Setting the weight sum is optional. See http://stackoverflow.com/questions/2698817/linear-layout-and-weight-in-android – xdumaine Nov 01 '12 at 17:02
  • 1
    I know, but I think it makes it a little more clearer what happens, AND the Views/Buttons keep their size if one of the Buttons has set its visibility to `GONE`. This may, or may not be intendet behavior – Rafael T Nov 01 '12 at 17:02