• Material Design 组件之 CollapsingToolbarLayout


    CollapsingToolbarLayout 主要用于实现一个可折叠的标题栏,一般作为 AppBarLayout 的子 View 来使用,下面总结一下 CollapsingToolbarLayout 的使用。

    Material Design 系列文章:

    常用属性

    //是否显示标题
    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"

    对于 Title 当折叠布局完全可见时 Title 变大,可折叠布局随着向上滑动可见范围变小 Title 也变小,可以通过如下方式设置 Title 的颜色,具体如下:

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

    这个 Title 的颜色渐变过程由 CollapsingToolbarLayout 完成,当然其他部分属性也可以在代码中设置。

    两个标志位

    单独在说一下两个重要属性,可以作为一个标志位来记:

    1. layout_scrollFlags
    2. layout_collapseMode

    layout_scrollFlags:一般使用 CoordinatorLayout、AppBarLayout等这些组件构成的界面,一般都有一个滚动视图,如 NestedScrollView,滚动视图一般设置了系统默认的 Behavior,具体如下:

    //设置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>

    要随着滚动视图移动的组件,如 ToolBar、CollapsingToolbarLayout 等需要设置 layout_scrollFlags 属性来指定不同的动作,关于 layout_scrollFlags 值的具体含义请参考之前的一篇文章: Material Design 组件之 AppBarLayout

    layout_collapseMode:layout_collapseMode 有两个值可以选择,如果设置了 pin 的 View 会随着页面向上滚动固定到顶部,如果设置了 parallax 的 View 会随着页面的滚动而滚动,此时可以结合另一个属性 layout_collapseParallaxMultiplier 形成视差滚动的效果。

    CollapsingToolbarLayout 的介绍就到此为止,没有案例当然是不可以,下面是一个简单的使用案列,布局如下:

    <?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>
    

    显示效果

    下面是显示效果,具体如下:

    CollapsingToolbarLayout

    可以选择关注微信公众号:jzman-blog 获取最新更新,一起交流学习!

  • 相关阅读:
    “花田喜事” 婚庆网站策划
    discuz 模块模板标签说明 DIY模块模板语法详解
    discuz x2.5 广告位开发学习(第二步:制作)
    DiscuzX2.5完整目录结构【模板目录template】
    Webservice 安全性访问
    X2.5 怎么关闭个人空间
    心中有佛,看人即佛;心中有屎,看人即屎
    discuz x2.5 广告位开发学习(第一步:摸索)
    UVA 128 Software CRC
    UVA 10791 Minimum Sum LCM
  • 原文地址:https://www.cnblogs.com/jzmanu/p/10284775.html
Copyright © 2020-2023  润新知