Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout weight not working as I thought

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?

like image 933
Neil Walker Avatar asked Sep 19 '13 14:09

Neil Walker


1 Answers

<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.

like image 197
Ahmed Ekri Avatar answered Nov 03 '22 16:11

Ahmed Ekri