2012-04-18 12 views
97

Tôi có một RelativeLayout bên trong một ScrollView. My RelativeLayout có android:layout_height="match_parent" nhưng chế độ xem không chiếm toàn bộ kích thước, nó giống như một wrap_content.Xem bên trong ScrollView không chiếm hết chỗ

Có cách nào để thực hiện hành vi fill_parent/match_parent thực không?

bố trí của tôi:

<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> 

Trả lời

324

Hãy thử thêm android:fillViewport="true" để scrollview của bạn

nhớ rằng android:layout_height=”fill_parent” nghĩa này rõ ràng không phải là những gì bạn muốn khi “thiết lập chiều cao với chiều cao của cha mẹ.” sử dụng ScrollView. Sau khi tất cả, các ScrollView sẽ trở thành vô ích nếu nội dung của nó luôn luôn là cao như chính nó. Để giải quyết vấn đề này, bạn cần sử dụng thuộc tính ScrollView có tên là android:fillViewport. Khi được đặt thành true, thuộc tính này sẽ khiến con của chế độ xem cuộn mở rộng đến độ cao của ScrollView nếu cần. Khi trẻ cao hơn ScrollView, thuộc tính không có hiệu lực.

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

+0

Nó hoạt động hoàn hảo. Tại sao? – Corbella

+1

Tương tự đối với HorizontalScrollView. – CoolMind

+0

Điều này cũng phù hợp với NestedScrollView (23.4)! –

1

Chỉ cần thêm android: fillViewport = "true" trong cách bố trí xml yout trong scrollview

<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>