2016-05-13 20 views
6

tôi thực hiện NonSwipeableViewPager với một mảnh có NestedScrollView như thế này, những gì tôi mong đợi là scrollview có thể cuộn lên và hiển thị 2 TextView:NestedScrollView không thể di chuyển với match_parent chiều cao con

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

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

      <include 
       android:id="@+id/header" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       layout="@layout/header" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:layout_marginBottom="16dp" 
       android:src="@drawable/ic_up" /> 

     </RelativeLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Text 1" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Text 2" /> 

    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

Nhưng nó không thể di chuyển, Tôi đã thử nhiều cách nhưng vẫn không nhận được bất kỳ giải pháp nào

+1

ScrollView cần chiều cao của con mình là wrap_content –

+0

@TimCastelijns vì vậy chúng tôi không có cách nào để làm điều đó với chiều cao match_parent, phải không? –

+0

đó chỉ là cách scrollview hoạt động, bạn có thể thiết lập scrollview để phù hợp với cha mẹ, nhưng không phải là con của scrollview, mà cần phải được quấn nội dung –

Trả lời

27
<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

Linearlayout này cần có android:layout_height="wrap_content".

Lý do cho điều đó là nếu con của scrollview có cùng kích thước với bản thân scrollview (cả hai chiều cao match_parent) có nghĩa là không có gì để cuộn qua vì chúng có cùng kích thước và scrollview sẽ chỉ cao như màn hình.

Nếu linearlayout có chiều cao wrap_content thì chiều cao không liên quan đến chiều cao của màn hình và scrollview sẽ có thể cuộn qua nó.

Chỉ cần nhớ rằng một scrollview chỉ có thể có 1 con trực tiếp, và đứa trẻ đó cần android:layout_height="wrap_content"

+0

Awesome người đàn ông! ......... –

+1

Đối với tôi, chỉ ' fillViewPort = true' được gọi. Chiều cao của trẻ không quan trọng trong trường hợp của tôi –

+0

fillViewPort = true cũng đã giải quyết nó cho tôi. Cảm ơn! –

-4

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
+1

u có thể giải thích thêm câu trả lời của bạn không? – Caipivara

1

Trong trường hợp của tôi app:layout_behavior="@string/appbar_scrolling_view_behavior" này chỉ được làm việc nếu một số vấn đề một mặt sẽ thử nó và có thể giải quyết vấn đề của bạn. bạn cũng nên thêm android:fillViewport="true" nhưng không có mã này hoạt động.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@drawable/subscription_background" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
Các vấn đề liên quan