2013-03-01 42 views
8

Tôi mới phát triển Android và tôi gặp vấn đề sau. Tôi cần sử dụng FrameLayout bên trong của ScrollView để làm cho sách có thể cuộn được. Tôi đã viết điều nàyFrameLayout bên trong ScrollView

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="#00ff00"> 

    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="500dp" 
     android:background="#ff0000"> 
    </FrameLayout> 
    </ScrollView> 

Nhưng điều này không hiệu quả. Tôi đã thử trong RelativeLayout và nó đã hoạt động, nhưng tôi cần sử dụng cho FrameLayout. Bất kỳ ý tưởng nào?

Trả lời

4

Tôi đã thử nhiều biến thể để giải quyết vấn đề này, bao gồm câu trả lời ở đây, nhưng không ai hữu ích. ScrollView luôn kết thúc tốt đẹp FrameLayout, nhưng mọi thứ đều bình thường với RelativeLayout, LinearLayout ...

Tôi đã tìm thấy một giải pháp cho điều này - để thiết lập chiều cao tối thiểu của FrameLayout. Bạn có thể đặt chiều cao tối thiểu động theo phương pháp setMinimumHeight().

2

Mẫu xml của bạn khá tốt, điều duy nhất tôi có thể thấy là: nếu phụ huynh ScrollLayout lớn hơn 500dp thì nó sẽ không cuộn.

Thật vậy, cuộn chỉ được sử dụng nếu nội dung lớn hơn chế độ xem cuộn.

đây, bạn thiết lập chiều cao nội dung của bạn để 500dip và bạn scrollview để 'match_parent' nếu cha mẹ của scrollview này mất toàn bộ màn hình, trên màn hình chiều cao 800dip (ví dụ)

=> cuộn là đơn giản là không cần thiết.

2

thiết lập Thử layout_height để "wrap_content" trong FrameLayout bạn

<FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff0000"> 

Nó sẽ di chuyển khi FrameLayout sẽ cao hơn so với màn hình biên giới. Hoặc nếu bạn muốn chiều cao cố định của vùng có thể cuộn, hãy đặt chiều cao thành 500dp trên ScrollView. Nó phụ thuộc vào những gì bạn muốn, nhưng không bao giờ thiết lập chiều cao cố định trên một con ScrollView. Chiều cao của con ScrollView luôn phải là wrap_content.

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="500dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="#00ff00"> 

    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff0000"> 
    </FrameLayout> 
</ScrollView> 
1

Để có được FrameLayout bên scrollview, làm điều này:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayoutHeader1" 
    android:layout_centerHorizontal="true" > 

    <LinearLayout 
     android:id="@+id/LinearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     <FrameLayout 
      android:id="@+id/FrameLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="1440dp" 
      > 
     </FrameLayout> 

    </LinearLayout> 
</ScrollView> 
13

android: fillViewport = "true" giải quyết vấn đề này.

android: fillViewport, Xác định xem cuộn có kéo dài nội dung của nó để lấp đầy chế độ xem hay không.

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </FrameLayout> 
    </ScrollView> 
Các vấn đề liên quan