fill_parent对ScrollView中的RelativeLayout不起作用

时间:2014-10-06 15:30:58

标签: android android-layout relativelayout

我在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 个答案:

答案 0 :(得分:10)

添加

android:fillViewPort="true"

ScrollView

fill_parent内的

match_parentScrollView高度实际上没有意义,它会被忽略 - 否则就不需要滚动。 fillViewPort="true"使滚动视图占据屏幕上可用的所有垂直空间。

答案 1 :(得分:-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" >