Android布局重量不像我想的那样工作

Nei*_*ker 8 android-layout

以下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>
Run Code Online (Sandbox Code Playgroud)

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

我究竟做错了什么?

Ahm*_*kri 27

<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>
Run Code Online (Sandbox Code Playgroud)

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

恩.如果你正在为宽度支持设置的重量,使用 android:layout_width="0dp"与一起android:layout_weight="3".另外,不要忘记android:weightSum="4"在父母中.