如何使ScrollView中的LinearLayout填满整个区域

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

    <ScrollView
        android:id="@+id/scrollView1"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/linear1"
            android:background="#FF0000"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:background="#00FF00"
                android:id="@+id/linear2"
                android:layout_width="match_parent"
                android:layout_height="200dip"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:background="#0000FF"
                android:id="@+id/linear3"
                android:layout_width="match_parent"
                android:layout_height="100dip"
                android:orientation="vertical" >
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

这是我的布局,我希望看到一个红色的背景(因为linear1有红色背景并具有填充父级的属性),还有两个带有绿色和蓝色背景的其他布局位于linear1之上

但实际上我看到的是来自scrollview的黑色背景以及来自linear2和linear3的绿色和蓝色背��,但没有来自linear1的红色背景

也就是说,linear的表现像android:layout_height="wrap_content"被设置而不是android:layout_height="match_parent"

有什么想法吗?

1个回答

89

您需要设置 fillViewport:

<ScrollView
    android:id="@+id/scrollView1"
    android:background="#000000"
    android:layout_width="match_parent"
    android:fillViewport="true" <!-- here -->
    android:layout_height="match_parent" >

    ...

 </ScrollView>

信息


4
如何在ScrollView中修复包含weightSum的LinearLayout? - Karthick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接