2016-05-13 155 views
6

我實現NonSwipeableViewPager一個片段具有NestedScrollView這個樣子,我想到的是,滾動視圖可以滾動起來,並顯示2個textviews:NestedScrollView無法match_parent高度子滾動

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

但它不能滾動,我嘗試了很多方法,但仍然沒有得到任何解決方案

+1

滾動型需要他們的孩子的身高被WRAP_CONTENT –

+0

@TimCastelijns,所以我們沒有任何的方式與match_parent高度來完成它,我們呢? –

+0

這就是滾動視圖的工作方式,你可以設置滾動視圖來匹配父窗口,但不是scrollview的子窗口,需要包裝內容 –

回答

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

此線性佈局應該有android:layout_height="wrap_content"

這樣做的原因是,如果scrollview的子級與scrollview本身的大小相同(高度均爲match_parent),則表示沒有任何可滾動的內容,因爲它們的大小相同,滾動視圖只會是像屏幕一樣高。

如果線性佈局的高度爲wrap_content那麼高度與屏幕高度無關,並且滾動視圖可以滾動瀏覽。

只要記住一個滾動視圖只能有1個直接孩子,而孩子需要android:layout_height="wrap_content"

+0

棒極了!......... –

+1

對我來說,只有' fillViewPort = true'啓動。孩子的身高在我的情況下並不重要 –

+0

fillViewPort = true也爲我解決了它。謝謝! –

-4

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

你能解釋更多的答案嗎? – Caipivara

1

在我的情況app:layout_behavior="@string/appbar_scrolling_view_behavior"這只是工作,如果有一個人面對問題,會嘗試也可能解決你的問題。你也應該添加android:fillViewport="true",但沒有這個我的代碼工作。

<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"> 
相關問題