2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:text="Button1"
        android:textSize="20dp" >
    </Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button2"
        android:textSize="20dp" >
    </Button>

</LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:text="Button1"
        android:textSize="20dp" >
    </Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Button2"
        android:textSize="20dp" >
    </Button>

</LinearLayout>

In code version 1 The height of button 1 is 5 times the button 2

In code version 2 the height of button 2 is 5 times the button 1

When android:layout_height="0dp" is replaced with android:layout_height="match_patent" the weights are reversed. Can you explain this behavior and how android:layout_weights actually works and explain the reason in each case.

4

1 に答える 1

2

Layout_weight は、値が 0 として指定されるコンポーネントに設定されます。これは、独自の高さ/幅がなく、重量で指定された画面の % を占めることを意味します。

于 2013-03-28T12:15:19.823 に答える