11

以下android:layout_weight 是什么意思?

我有这个:

<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 占据了四分之三的屏幕大小。

我究竟做错了什么?

4

2 回答 2

35
<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"父级中的 。

于 2013-09-19T14:07:57.613 回答
0

您的视图将占用的空间量计算为尺寸/布局重量,因此具有较低布局重量的视图在您的布局中占用更多空间。将来,您可能还需要使用 layout_weightSum。

他们在这里进一步解释

于 2013-09-20T05:49:17.697 回答