本文是codetrick上material design support library教程的第三篇,主要讲的是CollapsingToolbarLayout的概念和应用方法。

原文链接:Material Design Support Library Tutorial – Part 3

Collapsing Toolbar Layout

想实现工具栏collpasing的效果,需要将工具栏(Toolbar)包裹在CollapsingToolbarLayout内。布局的结构如下:

<android.support.design.widget.CoordinatorLayout >
<android.support.design.widget.AppBarLayout >
<android.support.design.widget.CollapsingToolbarLayout >
<ImageView />
<android.support.v7.widget.Toolbar />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- Your scrollable content here -->
</android.support.design.widget.CoordinatorLayout>

CoordinatorLayout

CoordinatorLayout是一个加强的FrameLayout,指定了子视图的多种交互行为。允许浮动的视图锚定在布局中。

AppBarLayout

AppBarLayout是一个垂直的线性布局,实现了material design中关于app bar概念的多个特性,也就是滚动手势。

子视图应该通过setScrollFlags(int)方法给出它们想要的滚动行为,相关联的布局xml属性是app:layout_scrollFlags

这个控件重度依赖CoordinatorLayout,而且需要是CoordinatorLayout的直接子控件。如果你把AppBarLayout放在别的ViewGroup中,它的大多数功能都将失效。

CollapsingToolbarLayout

CollapsingToolbarLayout是对Toolbar的包装,实现了一个可以收缩的应用栏。它被设计成作为AppBarLayout的直接子视图来使用。它包含以下的特性:

Collapsing title

这个标题在布局完全可见时最大,当布局滚出屏幕时会收缩变小。你可以通过setTitle(CharSequence)方法来设置标题。标题文本的外观可以通过collapsedTextAppearanceexpandedTextAppearance属性来修改。

Content scrim

一个full-bleed(满血?)的scrim(帘布?),当滚动位置到达一定阈值时会显示或者隐藏。你可以通过setContentScrim(Drawable)方法来改变它。

Status bar scrim

这也是一个scrim(帘布?),当滚动位置到达一定的阈值时会显示或隐藏在状态栏(status bar)后面。你可以通过setStatusBarScrim(Drawable)方法来改变它。在LOLLIPOP设备上,只有设置了fit system windows属性,它才有效。

Parallax scrolling children

在这个布局中,子视图可以选择视差滚动。参见COLLAPSE_MODE_PARALLAXsetParallaxMultiplier(float)

Pinned position children

子视图也可选择在全局空间中固定位置。在实现收缩效果时这很有用,因为即使布局在移动,它上面的Toolbar也可以固定在一个位置。

    <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:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"> <com.codentrick.materialdesigndemo.SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CollapsingToolbarLayout>

java代码:

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle("This is title");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));

相关代码和效果参见原作者的github库MaterialDesignSupportSample

Android material design support library -- CollapsingToolbarLayout简介的更多相关文章

  1. Android Meterial Design Support Library

    extends:http://inthecheesefactory.com/blog/android-design-support-library-codelab At the moment I be ...

  2. Android应用Design Support Library完全使用实例

    阅读目录 2-1 综述 2-2 TextInputLayout控件 2-3 FloatingActionButton控件 2-4 Snackbar控件 2-5 TabLayout控件 2-6 Navi ...

  3. 安卓高级3 Android应用Design Support Library完全使用实例

    原作者:http://www.open-open.com/lib/view/open1433385856119.html 1 背景 上周一年一度的Google IO全球开发者大会刚刚结束,Google ...

  4. 如何使用android design support library

    Android应用Design Support Library完全使用实例 - OPEN 开发经验库http://www.open-open.com/lib/view/open143338585611 ...

  5. Android Material Design 兼容库的使用

    Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...

  6. Material Design 开发利器:Android Design Support Library 介绍

    转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...

  7. Material Design with the Android Design Support Library

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

  8. Android Design Support Library使用详解

    Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...

  9. Android开发学习之路-Android Design Support Library使用(CoordinatorLayout的使用)

    效果图: 上面的这个图有两个效果是,一个是顶部的图片,在上滑之后会隐藏起来并且显示出一个ToolBar(ToolBar类似于ActionBar,但是只有ToolBar是兼容Material Desig ...

随机推荐

  1. 《Linux内核分析》 week2作业-时间片轮转

    一.基于时间片轮转调度代码的解读 代码结构主要由三个文件组成: 1.mypcb.h 2.myinterrupt.c 3.mymain.c 1.进程控制块(mypcb.h) /* CPU-specifi ...

  2. Oracle数据库之PL/SQL异常处理

    Oracle数据库之PL/SQL异常处理 异常指的是在程序运行过程中发生的异常事件,通常是由硬件问题或者程序设计问题所导致的. PL/SQL程序设计过程中,即使是写得最好的程序也可能会遇到错误或未预料 ...

  3. php global范例

    Example #1 $GLOBALS 范例 <?phpfunction test() {    $foo = "local variable"; echo '$foo in ...

  4. php加密解密实用类

    一个加解密类.如果你想在用户忘记密码时为他或她找回原来的密码,那么这个类是个好用的工具 用户注册的密码一般不会明文保存,总得加个密先.最简单的当然是在数据库sql语句中调用md5函数加密用户密码.这里 ...

  5. uva 10007 Count the Trees

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. 为ownCloud配置SSL连接

    为ownCloud配置SSL连接 在你开始使用ownCloud之前,强烈建议你在ownCloud中启用SSL支持.使用SSL可以提供重要的安全好处,比如加密ownCloud流量并提供适当的验证.在本教 ...

  7. ZOJ 2760 How Many Shortest Path

    题目大意:给定一个带权有向图G=(V, E)和源点s.汇点t,问s-t边不相交最短路最多有几条.(1 <= N <= 100) 题解:从源点汇点各跑一次Dij,然后对于每一条边(u,v)如 ...

  8. BZOJ1725: [Usaco2006 Nov]Corn Fields牧场的安排

    1725: [Usaco2006 Nov]Corn Fields牧场的安排 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 400  Solved: 290 ...

  9. 黑马程序员_JavaIO流(四)

    File概述 File类 用来将文件或者文件夹封装成对象. 方便对文件与文件夹的属性信息进行操作. File对象可以作为参数传递给流的构造函数. 了解File类中的常用方法. 字段:static St ...

  10. MongoDB开发学习(1)开天辟地,经典入门

    原文地址:http://www.cnblogs.com/xumingxiang/archive/2012/04/08/2437468.html 如果你从来没有接触MongoDB或对MongoDB有一点 ...