【问题标题】:CollapsingToolbarLayout not collapsing when using ScrollView (and NestedScrollView)使用 ScrollView(和 NestedScrollView)时 CollapsingToolbarLayout 不折叠
【发布时间】:2015-07-16 10:29:59
【问题描述】:

我正在尝试将 CollapsingToolbarLayout 与 ScrollView 一起使用,但无法正常工作。我试试这个:

<android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_below="@+id/toolbar"
                android:scrollbars="none"
                android:overScrollMode="never"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:behavior_overlapTop="192dp"
                android:layout_height="match_parent">

            <LinearLayout android:layout_width="match_parent"
                          android:orientation="vertical"
                          android:padding="@dimen/medium_padding"
                          android:layout_height="match_parent">

                          <!-- my content -->


            </LinearLayout>

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



        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>
        <ImageView
                android:src="@drawable/fondo_drawer"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"/>

    </android.support.design.widget.CollapsingToolbarLayout>

我用 ScrollView 和 NestedScrollView 测试失败。

有什么想法吗?提前致谢!

【问题讨论】:

    标签: android androiddesignsupport android-coordinatorlayout


    【解决方案1】:

    我觉得

    android:fitsSystemWindows="true" is the cause.
    

    查看我的简单工作示例代码。

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/product_detail_main_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:apptools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/product_detail_appBar_height"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/detail_product_collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:foregroundGravity="bottom|right"
                android:foregroundTintMode="add"
                android:clipToPadding="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="@dimen/space_xxlarge"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/image"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7"/>
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/product_toolBar_title"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="pin"
                  app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
               app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    
    </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.NestedScrollView
     ........
    <android.support.design.widget.CoordinatorLayout
    

    您可以查看此链接http://inthecheesefactory.com/blog/android-design-support-library-codelab/en

    【讨论】:

    • 是的!你的例子工作正常。我想我需要在 CoordinatorLayout 中介绍所有内容!谢谢!!
    【解决方案2】:

    我遇到了同样的错误abhishek's 答案很完美。

    我想补充一点。如果您只为 AppBarLayout 执行 android:fitsSystemWindows="true" 它将起作用,您不必为 ImageViewToolBar 设置它 - 因为它们是 AppBarLayout 的一部分,这也适用于它们。

    看看这里 - https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.an1hvz44l

    系统窗口是系统正在绘制的屏幕部分 非交互式(在状态栏的情况下)或交互式 (在导航栏的情况下)内容。

    大多数情况下,您的应用不需要在状态栏或 导航栏,但如果你这样做:你需要确保交互 元素(如按钮)不会隐藏在它们下面。这就是 android:fitsSystemWindows=“true” 属性的默认行为 给你:它设置视图的填充以确保内容 不要覆盖系统窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多