Collapsingtoolbarlayout of material design components

Collapsingtoolbarlayout is mainly used to implement a collapsible title bar, which is generally used as a child view of appbarlayout. The following is a summary of the use of collapsingtoolbarlayout.

Material design series:

//是否显示标题
app:titleEnabled="true"
//标题内容
app:title="CollapsingToolbarLayout"
//扩展后Title的位置
app:expandedTitleGravity="left|bottom"
//收缩后Title的位置
app:collapsedTitleGravity="left"
//CollapsingToolbarLayout收缩后Toolbar的背景颜色
app:contentScrim ="@color/colorPrimary"
//CollapsingToolbarLayout收缩时颜色变化的持续时间
app:scrimAnimationDuration="1200"
//颜色从可见高度的什么位置开始变化
app:scrimVisibleHeightTrigger="150dp"
//状态颜色变化(Android 5.0)
app:statusBarScrim="@color/colorAccent"
//设置滑动组件与手势之间的关系
app:layout_scrollFlags="scroll|exitUntilCollapsed"

For the title, when the folded layout is fully visible, the title becomes larger, and the visible range of the foldable layout becomes smaller as it slides upward. You can set the color of the title in the following ways, as follows:

//设置标题
ctlCollapsingLayout.setTitle("CollapsingToolbarLayout");
//设置CollapsingToolbarLayout扩展时的颜色
ctlCollapsingLayout.setExpandedTitleColor(Color.parseColor("#ffffff"));
//设置CollapsingToolbarLayout收缩时的颜色
ctlCollapsingLayout.setCollapsedTitleTextColor(Color.parseColor("#46eada"));

The color gradient process of this title is completed by collapsingtoolbarlayout. Of course, other properties can also be set in the code.

Let's talk about two important attributes separately, which can be recorded as a flag bit:

layout_ Scrollflags: generally, the interface is composed of coordinatorlayout, appbarlayout and other components. Generally, there is a rolling view, such as nestedscrollview. The rolling view generally sets the system default behavior, as follows:

//设置layout_behavior属性
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    ...
</android.support.v4.widget.NestedScrollView>

Layout needs to be set for components to move with the scrolling view, such as toolbar, collapsing toolbar layout, etc_ Scrollflags attribute to specify different actions, about layout_ For the specific meaning of the scrollflags value, please refer to the previous article: appbarlayout of material design components.

layout_ collapseMode:layout_ Collapsemode has two values to choose from. If pin is set, the view will scroll up and fixed to the top with the page. If parallax is set, the view will scroll with the page. At this time, another attribute layout can be combined_ Collapseparallelaxmultipler forms the effect of parallax scrolling.

That's all for the collapsingtoolbarlayout. Of course, you can't do it without a case. The following is a simple use case list. The layout is as follows:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manu.materialdesignsamples.samples.SampleCollapsingToolbarLayoutActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/ctlCollapsingLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:titleEnabled="true"
            app:title="CollapsingToolbarLayout"
            app:expandedTitleGravity="left|bottom"
            app:collapsedTitleGravity="left"
            app:contentScrim ="@color/colorPrimary"
            app:scrimAnimationDuration="1200"
            app:scrimVisibleHeightTrigger="150dp"
            app:statusBarScrim="@color/colorAccent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.6"
                android:background="@drawable/ic_collapsing_title"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin">
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvCollapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

The following is the display effect, as follows:

You can choose to pay attention to WeChat official account: jzman-blog get the latest updates, and exchange learning together!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>