Android layout_weight没有像我想的那样工作

时间:2013-09-19 14:05:03

标签: android-layout

关注What does android:layout_weight mean?

我有这个:

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

链接告诉我buttonToastFancy将占用大小的四分之三(3 /(3 + 1),但反之亦然。按照Eclipse /我的AVD,buttonToastSimple占据屏幕大小的四分之三。< / p>

我做错了什么?

2 个答案:

答案 0 :(得分:25)

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

尝试将所需属性设置为0dp ..

离。如果您要为宽度支持设置权重,请使用android:layout_width="0dp"android:layout_weight="3"。另外,不要忘记父母中的android:weightSum="4"

答案 1 :(得分:0)

您的视图占用的空间量计算为 dimension / layout_weight因此layout_weight较低的视图在布局中占用更多空间。在将来,您可能还需要用户layout_weightSum。

他们是further explained here