ScrollView中的视图没有占满整个空间。

165

我在 ScrollView 中使用了一个 RelativeLayout。 我的 RelativeLayout 设置了 android:layout_height="match_parent",但是视图并没有占据整个空间,表现得像是 wrap_content。

有没有方法可以实现真正的 fill_parent/match_parent 行为?

我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <ImageView
    android:id="@+id/top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/top" />

    <ImageView
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/top"
    android:scaleType="fitXY"
    android:src="@drawable/headerbig" />

    <ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/header"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="3dp"
    android:src="@drawable/logo" />

    <ScrollView
    android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom"
    android:layout_below="@+id/logo"
    android:background="@drawable/background" >

        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

             <ImageView
            android:id="@+id/dashboard01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/dashboard02"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:src="@drawable/dashboard01"
            android:visibility="visible" />

            <ImageView
            android:id="@+id/dashboard02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/dashboard02" />

             <ImageView
            android:id="@+id/dashboard03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/dashboard02"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:src="@drawable/dashboard03"
            android:visibility="visible" />
         </RelativeLayout>
    </ScrollView>

    <ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/logo"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY"
    android:src="@drawable/bar" />

    <ImageView
    android:id="@+id/bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:scaleType="fitXY"
    android:src="@drawable/bottom" />

</RelativeLayout>
2个回答

509
尝试在您的ScrollView中添加android:fillViewport="true"属性。请注意,android:layout_height="fill_parent"的意思是“将高度设置为父元素的高度”,但在使用ScrollView时显然不是您想要的。毕竟,如果其内容总是与自身一样高,则ScrollView将变得无用。为解决此问题,您需要使用ScrollView属性称为android:fillViewport。当该属性设置为true时,如果需要,它会导致滚动视图的子项扩展到ScrollView的高度。当子项高于ScrollView时,该属性无效。参考链接:http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

4
同样适用于HorizontalScrollView。 - CoolMind
如果它对你不起作用,请确保在ScrollView上设置属性,而不是子元素! - Enselic

17

只需在ScrollView的xml布局中添加android:fillViewport="true"即可。

<ScrollView android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/green"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:fillViewport="true"
    >

<!--define views here-->


</ScrollView>

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