【问题标题】:When using the CoordinatorLayout my ScrollView has an incorrect size使用 CoordinatorLayout 时,我的 ScrollView 大小不正确
【发布时间】:2015-08-14 23:02:33
【问题描述】:

我在布局中使用 ScrollView,并尝试使用 design support library 中的新 CoordinatorLayout

我的布局文件如下所示:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
      ...
    </LinearLayout>
  </ScrollView>
  <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar ... />
  </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

测试时,滚动视图只占据屏幕的一部分。出了什么问题?

【问题讨论】:

    标签: android android-support-library material-design android-scrollview android-design-library


    【解决方案1】:

    标准的 ScrollView 仅用作父级。您需要将 ScrollView 更改为 android.support.v4.widget.NestedScrollView

    可以在AppBarLayout 的参考文档中看到一个示例。

    【讨论】:

      【解决方案2】:

      NestedScrollView 和 ScrollView 一样,但它支持充当 新旧版本中的嵌套滚动父项和子项 安卓的。默认启用嵌套滚动。

      您可以在父 ScrollView 中使用 NestedScrollView。当需要在另一个滚动视图中使用滚动视图时,使用 NestedScrollView。当系统需要决定滚动哪个视图时,这很有用。

      这里是一个带有 CoordinatorLayout 的 NestedScrollView 示例:

       <android.support.design.widget.CoordinatorLayout
               xmlns:android="http://schemas.android.com/apk/res/android"
               xmlns:app="http://schemas.android.com/apk/res-auto"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
      
           <android.support.v4.widget.NestedScrollView
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
               <!-- Your scrolling content -->
      
           </android.support.v4.widget.NestedScrollView>
      
           <android.support.design.widget.AppBarLayout
                   android:layout_height="wrap_content"
                   android:layout_width="match_parent">
      
               <android.support.v7.widget.Toolbar
                       ...
                       app:layout_scrollFlags="scroll|enterAlways"/>
      
               <android.support.design.widget.TabLayout
                       ...
                       app:layout_scrollFlags="scroll|enterAlways"/>
      
           </android.support.design.widget.AppBarLayout>
      
       </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-08
        • 1970-01-01
        相关资源
        最近更新 更多