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

Material Design 系列文章:

常用属性

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

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

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

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

两个标志位

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

  1. layout_scrollFlags
  2. layout_collapseMode

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

  1. //设置layout_behavior属性
  2. <android.support.v4.widget.NestedScrollView
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  6. ...
  7. </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 的介绍就到此为止,没有案例当然是不可以,下面是一个简单的使用案列,布局如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context="com.manu.materialdesignsamples.samples.SampleCollapsingToolbarLayoutActivity">
  9. <android.support.design.widget.AppBarLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="200dp">
  12. <android.support.design.widget.CollapsingToolbarLayout
  13. android:id="@+id/ctlCollapsingLayout"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. app:titleEnabled="true"
  17. app:title="CollapsingToolbarLayout"
  18. app:expandedTitleGravity="left|bottom"
  19. app:collapsedTitleGravity="left"
  20. app:contentScrim ="@color/colorPrimary"
  21. app:scrimAnimationDuration="1200"
  22. app:scrimVisibleHeightTrigger="150dp"
  23. app:statusBarScrim="@color/colorAccent"
  24. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  25. <ImageView
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. app:layout_collapseMode="parallax"
  29. app:layout_collapseParallaxMultiplier="0.6"
  30. android:background="@drawable/ic_collapsing_title"/>
  31. <android.support.v7.widget.Toolbar
  32. android:id="@+id/toolbar"
  33. android:layout_width="match_parent"
  34. android:layout_height="70dp"
  35. android:minHeight="?attr/actionBarSize"
  36. app:layout_collapseMode="pin">
  37. </android.support.v7.widget.Toolbar>
  38. </android.support.design.widget.CollapsingToolbarLayout>
  39. </android.support.design.widget.AppBarLayout>
  40. <android.support.v4.widget.NestedScrollView
  41. android:layout_width="match_parent"
  42. android:layout_height="match_parent"
  43. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  44. <android.support.v7.widget.RecyclerView
  45. android:id="@+id/rvCollapsing"
  46. android:layout_width="match_parent"
  47. android:layout_height="match_parent"/>
  48. </android.support.v4.widget.NestedScrollView>
  49. </android.support.design.widget.CoordinatorLayout>

显示效果

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

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

Material Design 组件之 CollapsingToolbarLayout的更多相关文章

  1. Material Design 组件之NavigationView

    今天来看一下 NavigationView 的使用,NavigationView 是一个标准的导航菜单,其菜单内容由菜单资源文件来填充,NavigationView 一般和 DrawerLayout ...

  2. Android material design support library -- CollapsingToolbarLayout简介

    本文是codetrick上material design support library教程的第三篇,主要讲的是CollapsingToolbarLayout的概念和应用方法. 原文链接:Materi ...

  3. Material Design 组件之 AppBarLayout

    AppBarLayout 是一个垂直方向的 LinearLayout,它实现了许多符合 Material Design 设计规范的状态栏应该具有的功能,比如滚动手势. AppBarLayout 一般直 ...

  4. Material Design 组件之 FloatingActionButton

    Material Design 设计规范在 Google I/O 2014 推出,这种设计理念一经推出就受到广大开发者的喜爱,主要侧重于纸墨化创作和突出设计的实体感,使得设计更接近于真实世界,力求平滑 ...

  5. Material Design with the Android Design Support Library

    Material Design with the Android Design Support Library 原文http://www.sitepoint.com/material-design-a ...

  6. Jquery之家5个顶级Material Design框架

    谷歌Material Design在如今的前端页面设计中非常流行.Material Design的设计风格向我们展示了一个简单而有内涵的现代UI设计方案. Material Design是如此的简洁美 ...

  7. Top 15 - Material Design框架和类库(译)

    _Material design_是Google开发的,目的是为了统一公司的web端和手机端的产品风格.它是基于很多的原则,比如像合适的动画,响应式,以及颜色和阴影的使用.完整的指南详情请看这里(ht ...

  8. Material Design Lite,简洁惊艳的前端工具箱 之 交互组件。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, 博客地址为http://www.cnblogs.com/jasonnode/ . 网站上有对应 ...

  9. Material Design - CollapsingToolbarLayout

    一.概述 CollapsingToolbarLayout是Material Design的一个Layout,直接继承于FrameLayout. 二.使用 1.导包 2.使用 - 设置相关属性 三.参考 ...

随机推荐

  1. All In One

    set1 https://github.com/tianhang-f... set2 https://github.com/tianhang/F... set3https://github.com/t ...

  2. 前端模板引擎doT.js的使用

    前言 我们在做前端开发时,经常需要根据后台返回的json数据动态生成html并插入到页面中显示.最简单的方法就是通过jQuery去遍历数据拼接html,如以下: <script> var ...

  3. LeetCode 278.First Bad Version(E)(P)

    题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...

  4. 修复Nginx的WebDAV功能

    如果想使用WebDAV来实现文件共享,尤其是想使用操作系统内置功能来挂载文件系统的话,省心的话还是用Apache吧. 下文介绍如何用Nginx来实现这个目标.Windows内置的客户端是Microso ...

  5. python基本数据类型的操作

    1 列表和元组 1.列表基本操作 1. 列表赋值 a = [1,2,3,4,5,6,7,8] a[0] = 100 #the result : [100, 2, 3, 4, 5, 6, 7, 8] 2 ...

  6. jwt的token如何使用

    JWT简介: JWT(JSON WEB TOKEN):JSON网络令牌,JWT是一个轻便的安全跨平台传输格式,定义了一个紧凑的自包含的方式在不同实体之间安全传输信息(JSON格式).它是在Web环境下 ...

  7. [转载]Linux服务器丢包故障的解决思路及引申的TCP/IP协议栈理论

    Linux服务器丢包故障的解决思路及引申的TCP/IP协议栈理论 转载至:https://www.sdnlab.com/17530.html 我们使用Linux作为服务器操作系统时,为了达到高并发处理 ...

  8. [红日安全]Web安全Day12 – 会话安全实战攻防

    本文由红日安全成员: ruanruan 编写,如有不当,还望斧正. 大家好,我们是红日安全-Web安全攻防小组.此项目是关于Web安全的系列文章分享,还包含一个HTB靶场供大家练习,我们给这个项目起了 ...

  9. 【Python】2.12学习笔记 变量

    变量 关于变量我有一个不能理解的,关于全局变量作用域与地址的问题,学函数的时候我可能会搞懂它并且写下来 另外,其实昨天说的是有些不准确的,\(Python\)里的变量不是不用声明类型,只是声明方式特殊 ...

  10. 还记得第一个看到的Flutter组件吗?

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 MaterialApp 在学习Flutter的过程中我们第 ...