ScrollView中RelativeLayout的fill_parent属性不起作用

4

我在ScrollView中使用了一个RelativeLayout,并将其设置为fill_parent。但是,它只填充水平方向,而不是垂直方向。我想要在RelativeLayout中添加更多内容,但首先需要弄清楚为什么RelativeLayout不能填充ScrollView的高度。

感谢任何帮助。

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/gray"
    android:layout_below="@+id/previewButton"
    android:layout_marginTop="10dp" >

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_below="@+id/matchingWordsLabel"
        android:background="@color/white"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        >

        <TextView
            android:id="@+id/titleLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="Title:"
            />

            <EditText
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/titleLabel"
            android:layout_alignBaseline="@+id/titleLabel"
            android:ems="10" />
    </RelativeLayout>
</ScrollView>
2个回答

10

添加

android:fillViewPort="true"

针对 ScrollView,使用 fill_parentmatch_parent 属性设置高度并没有实际意义,也会被忽略 - 否则就没有必要滚动了。使用 fillViewPort="true" 可以让滚动视图占据屏幕上所有可用的垂直空间。


-1

由于ScrollView只能有一个子元素,因此在其中放置一个LinearLayout。在LinearLayout中放置RelativeLayout。将高度设置为wrap_content。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/svRecord"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:orientation="vertical"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:visibility="visible" >

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