12

following What does android:layout_weight mean?

I have this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/buttonToastSimple"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="English Toast" />

    <Button
        android:id="@+id/buttonToastFancy"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="French Toast" />

</LinearLayout>

The link tells me buttonToastFancy will take up three quarters of the size (3/(3+1), but it's the other way round. The buttonToastSimple takes up three quarters of the screen size according to Eclipse/my AVD.

What am I doing wrong?

Community
  • 1
  • 1
Neil Walker
  • 6,400
  • 14
  • 57
  • 86

2 Answers2

36
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:weightSum="4"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/buttonToastSimple"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="English Toast" />

    <Button
        android:id="@+id/buttonToastFancy"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="French Toast" />

</LinearLayout>

try setting the wanted attribute to 0dp ..

ex. if you are setting the weight for the widths support, use android:layout_width="0dp" along with the android:layout_weight="3". Also, don't forget the android:weightSum="4" in the parent.

Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
  • That worked a treat, thanks. What was happening with my 'fill_parent' value to make it reverse everything? – Neil Walker Sep 19 '13 at 14:10
  • welcome, can you please accept and upvote the answer if it worked. because fill parent means to fill the available space, so when you are setting it to **1** and there is more space availabe it'll take it, rather than following its weight. – Ahmed Ekri Sep 19 '13 at 14:13
  • Setting width values to 0dp was just the thing that solved my problem. Thanks! – Nanoturka Oct 08 '17 at 12:35
0

the amount of space your view is going to occupy is computed as dimension/layout_weight hence the view with the lower layout_weight occupies more space in your layout. in the future, you may also need to user layout_weightSum.

They are further explained here.

Community
  • 1
  • 1
chjarder
  • 614
  • 6
  • 10