Android layout_weight 未按预期工作

Android layout_weight is not working as expected

布局权重在这段代码中似乎不起作用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Header -->

    <include
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_height"
        layout="@layout/header" />

    <!-- Layout -->

    <ScrollView
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/light_blue"
            android:orientation="vertical" >

            <LinearLayout
                android:weightSum="10"
                android:layout_width="match_parent"
                android:layout_height="@dimen/home_first_layout"
                android:layout_margin="5dp"
                android:background="@android:color/white"
                android:orientation="horizontal" >

                <LinearLayout
                    android:background="@android:color/background_dark"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:orientation="vertical" >


                </LinearLayout>

                <LinearLayout
                    android:background="@android:color/background_light"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout
                    android:layout_weight="2"
                    android:background="@android:color/background_dark"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <!-- <ImageView
                        android:gravity="end"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/upcoming" /> -->
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>
    </ScrollView>

</LinearLayout>

home_first_layout 包含三个水平布局。它们已被分配布局权重。你们能解释一下为什么布局权重没有显示预期的结果吗?

截图如下:

在布局中指定权重时,您还必须将加权视图的 height/width 设置为 0dp。还有一件事,您需要将线性布局的 layout_height 设置为 fill_parent。你写成wrap_content。这就是为什么没有清楚显示权重的原因,因为线性布局中没有任何视图(所以它们的 layout_height 就像 0dp - 它们被隐藏了)。

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

    <!-- Header -->

    <include
        layout="@layout/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_height" />

    <!-- Layout -->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/light_blue"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/home_first_layout"
                android:layout_margin="5dp"
                android:background="@android:color/white"
                android:orientation="horizontal"
                android:weightSum="10"
                android:baselineAligned="false">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="3"
                    android:background="@android:color/background_dark"
                    android:orientation="vertical">


                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="5"
                    android:background="@android:color/background_light"
                    android:orientation="vertical"></LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:background="@android:color/background_dark"
                    android:orientation="vertical">

                    <!-- <ImageView
                        android:gravity="end"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/upcoming" /> -->
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>