本文是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. [转]extern,static存储空间矛盾

    其实,这两个语句的位置不同,会出现不同的解释.这主要是由于 static 具有的两重意义所导致的: (1) 如果 static int foo; 这一句位于函数中,则 static 表示的是存储属性, ...

  2. Input File 表单上传按钮美化

    HTML <div class="input-file-button"> 上传图片<input type="file" class=" ...

  3. Thinkphp 控制器

    控 制 器: 1.命名方法:新建一个主页面的控制器 controller文件夹下新建一个文件,命名为:MainController.class.php首字母都大写,称为驼峰命名法 eg: <?p ...

  4. 关于 Delphi 中的Sender和易混淆的概念(转)

    /////////////////////////////////////////////////////// Delphi 中Sender对象的定义///////////////////////// ...

  5. Java Tomcat 中调用.net DLL的方法

    近日一个java的项目,客户要求项目中必须使用其提供的加密机制,扔给了两个.net写的DLL.网络上搜了一圈也没找到啥东西,甚至看到人扬言此事绝无可能.郁闷当中考虑了一个思路.用C#做一个Com,调用 ...

  6. 搭建本地Nuget服务器并使用NuGet Package Explorer工具打包nuget包

    1.什么是Nuget: 百度百科描述: Nuget是 ASP .NET Gallery 的一员.NuGet 是免费.开源的包管理开发工具,专注于在 .NET 应用开发过程中,简单地合并第三方的组件库. ...

  7. win7禁止自动使用浏览器打开FTP而是用资源管理器

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\ftp] @="URL:File Transfer Protocol" ...

  8. MSP430单片机的中断

    这篇文章是从网上转载过来的,原文章地址:http://www.21ic.com/jichuzhishi/mcu/questions/2012-12-21/154794.html 中断是MSP430微处 ...

  9. 淺析LED、LED背光、OLED的技術原理與區別

    眼下很多廠商在推廣自己產品的時候都偷換了概念.明明是LED背光顯示器卻要簡稱為LED顯示器.事實上LED顯示器和目前的LED背光顯示器有著本質的區別.當然容易讓大家混淆的還有個技術非常先進的OLED. ...

  10. 51单片机I/O口使用经验

    按常规,在51端口(P1.P2.P3)某位用作输入时,必须先向对应的锁存器写入1,使FET截止.一般情况是这样,也有例外.所谓IO口内部与电源相连的上拉电阻而非一常规线性电阻,实质上,该电阻是由两个场 ...