本文是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. 快速幂:quickpow

    众所周知,快速幂是优化对数的次方运算的最普遍手段.在学习快速幂的思想时,其分治思想容易让大家用简单的递归实现. 但其实,除了递归之外,更好的方法会是简单的 WHILE循环.下面贴代码: #includ ...

  2. Helloworld和程序员人生

    转:Helloworld和程序员人生 高中时期 10 PRINT "HELLO WORLD" 20 END 大学新生 program Hello(input, output) be ...

  3. Menu之选项菜单

    Android有三种形式的菜单:选项菜单(optionMenu).上下文菜单(ContextMenu).子菜单(subMenu).最常用的是选项菜单,该菜单在点击menu按键后会在对应的Activit ...

  4. Linux iptables 应用控制访问SSH服务

    Title:Linux iptables 应用控制访问SSH服务  --2012-02-23 17:51 今天用到了以前从来没有用到过的,iptables控制访问,只允许外部访问SSH服务(22号端口 ...

  5. Protel 的自动推挤功能

    大家都在用PROTEL99SE...有时候要修改布好的线...一条条的去拆,这样很麻烦.其实PROTEL99SE是有推挤功能的...虽然不是很强...但是可以给大家带来方便.下面我就发个自己制作的教程 ...

  6. OCX控件的注册卸载,以及判断是否注册

    方法一:在dos或Windows命令行下运行:regsvr32 ocxname.ocx 注册 示例:regsvr32 netshare.ocx     //注册netshare.ocx控件regsvr ...

  7. Remote Desktop Connection Manager 多个远程管理

    http://www.microsoft.com/en-us/download/details.aspx?id=21101 Remote Desktop Connection Manager 多个远程 ...

  8. HDU_2044——蜜蜂走蜂房,递推

    Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示.   Input 输入数据的 ...

  9. 使用MapReduce将HDFS数据导入到HBase(一)

    package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...

  10. 给大家推荐一款代替Visio的在线作图工具ProcessOn

    过去作图的时候一直都是在用visio,每一次换了电脑使用都要重新安装,这大家都知道,最头疼的就是激活问题,曾经因为激活问题我“找遍了”正个互联网,最后还没找到...从08年开始到现在,visio用了这 ...