修复下方的工具栏顶部和标题

时间:2017-04-04 09:11:06

标签: android android-layout toolbar

我有一个标题和一个工具栏。 向上滚动标题时,工具栏将固定在顶部。

问题是,我希望在标题之前的工具栏,在我的活动之上,当我向上滚动标题时,工具栏只会改变它的颜色。

toolbar
header (when scroll, change toolbar color)

任何想法如何在标题之前添加工具栏,在我的活动之上并在向上滚动标题时更改其颜色?

<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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <RelativeLayout android:id="@+id/header"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="0dp"
            android:id="@+id/imageViewProfile"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

        </RelativeLayout>


            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="?attr/colorPrimaryDark" />


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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="none"
        android:layout_alignParentLeft="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

1 个答案:

答案 0 :(得分:1)

试试这个:

<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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        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"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:background="@color/colorAccent"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:padding="0dp"
                    android:id="@+id/imageViewProfile"
                    android:adjustViewBounds="true"
                    android:cropToPadding="false"
                    android:layout_centerHorizontal="true"
                    android:src="@mipmap/ic_launcher"/>

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="?attr/colorPrimaryDark" />
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <!--
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="none"
        android:layout_alignParentLeft="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        -->
    <include layout="@layout/content_scrolling"></include>
</android.support.design.widget.CoordinatorLayout>

<强>输出

enter image description here

希望这会有所帮助〜