2016-05-13 163 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"> 
相关问题