android: ScrollView parent无法解析权重

android: ScrollView parent can't resolve the weight

我有一个奇怪的问题。正如您在下面的代码中看到的,对于 hor_Ln,我已将 android:layout_weight 设置为 50,因此它应该适合屏幕高度的 50% 以填充按钮的高度,但事实并非如此。

尽管如果我将 ScrollView 更改为 LinearLayout 作为父布局,它看起来是原始且正确的。

这是我的代码:

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


    <LinearLayout
        android:id="@+id/vertical_Ln"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:weightSum="100"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/hor_Ln"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:weightSum="10"
            android:layout_weight="50">

            <Button
                android:layout_width="0dp"
                android:layout_weight="5"
                android:layout_height="match_parent" />


        </LinearLayout>

    </LinearLayout>

</ScrollView>

这是输出:

有什么问题?

您必须包含 属性 android:fillViewport="true" 才能让您的 ScrollView 全屏显示。

在xml中设置android:fillViewport="true"

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >
   <LinearLayout
    android:id="@+id/vertical_Ln"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:weightSum="100"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/hor_Ln"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:weightSum="10"
        android:layout_weight="50">

        <Button
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="match_parent" />


    </LinearLayout>

    </LinearLayout>

  </ScrollView>