可折叠的工具栏布局不会折叠android

时间:2016-09-08 10:18:43

标签: android android-collapsingtoolbarlayout

这是我的xml文件。滚动toobar根本不滚动。它仍然是粘滞的,只有回收者视图滚动的元素

<CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
       >

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/collappbar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >
            <View
                android:id="@+id/image"
                android:layout_width="fill_parent"
                android:layout_height="192dp"
                app:layout_collapseMode="parallax"
                android:background="@android:color/transparent"
                />
            <include layout="@layout/toolbar" />
              </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>
    <ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f4f4f4"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_gravity="bottom"
        android:layout_marginBottom="50dp"
        android:background="@color/light_gray"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabTextColor="@color/gray"
        app:tabTextAppearance="@style/tabLayoutStyle"
        app:tabSelectedTextColor="@color/selected_text_color"
        app:tabIndicatorColor="@color/more_comment_color"
        android:background="@android:color/background_light"
        android:layout_gravity="bottom"
        />

</CoordinatorLayout>

可折叠工具栏没有折叠。请帮我找出代码中缺少的内容

2 个答案:

答案 0 :(得分:0)

像这样使用它会起作用:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/backgroundlayout"
            android:fitsSystemWindows="true"
            app:collapsedTitleGravity="left"
            app:contentScrim="#ffffff"
            app:expandedTitleMarginEnd="0dp"
            app:expandedTitleMarginStart="0dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <View
                android:id="@+id/image"
                android:layout_width="fill_parent"
                android:layout_height="192dp"
                android:background="@android:color/transparent"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlwaysCollapsed" />


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin" />


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

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:0)

请参阅以下链接,

https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

仅当内容视图具有RecyclerView或任何其他能够嵌套滚动的视图(如NestedScrollView)时,Appbar行为才有效。