CoordinatorLayout是Android 5.0新特性——Material Design中的一个布局控件,主要用来协调各个子视图之间的工作,也可以用来作为顶部布局。CoordinatorLayout主要是通过设置子视图的Behavior来达到协调子视图之间的工作的目的的。

  CoordinatorLayout主要有以下三种最常用的使用方法:

  • CoordinatorLayout + FloatingActionButton实现FAB与其他视图的协调;
  • CoordinatorLayout + AppBarLayout实现头部布局与滑动视图的协调;
  • CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout实现头部布局与滑动视图的协调,以及头部布局内部各视图的协调。

1、CoordinatorLayout + FloatingActionButton:

  CoordinatorLayout为嵌套其中的子视图提供了两个属性:layout_anchor和layout_anchorGravity,前者是用来设置当前视图的锚点视图,即当前视图停靠在哪个视图上;后者是用来设置当前视图与锚点视图的位置关系,即当前视图停靠在锚点视图的哪个位置,这两个属性最常用的控件就是FAB。例如,下面这段代码中的两个FAB,一个停靠在整个CoordinatorLayout布局的右下角,另一个停靠在Toolbar的右下角:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_fab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary" /> <!-- CoordinatorLayout作为一个super-powered FrameLayout,通过设置子View的layout_gravity来设置子View的位置 -->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_margin="20.0dip"
android:src="@mipmap/ic_launcher"
app:fabSize="normal" /> <!--
CoordinatorLayout为子View提供了一个anchor属性,可以设置锚点,即固定在某个控件的某个位置
app:layout_anchor:设置锚点控件
app:layout_anchorGravity:设置将当前控件固定在目标控件的哪个位置
-->
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50.0dip"
android:src="@mipmap/ic_launcher"
app:fabSize="normal"
app:layout_anchor="@id/toolbar"
app:layout_anchorGravity="bottom|right" /> </android.support.design.widget.CoordinatorLayout>

  运行结果如图所示:

2、CoordinatorLayout + AppBarLayout:

  AppBarLayout是为了MD设计的App Bar,支持手势滑动操作。被AppBarLayout包裹的子视图会具有layout_scrollFlags属性,这个属性是用来控制子视图的滑动模式的,有以下几个可选值:

  • scroll:所有想滚动出屏幕的view都需要设置这个flag,没有设置这个flag的view将被固定在屏幕顶部。
  • enterAlways:设置这个flag时,向下的滚动都会导致该view变为可见,启用“快速返回模式”。
  • enterAlwaysCollapsed:当你的视图已经设置minHeight属性又使用此标志时,你的视图只能以最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度。
  • snap:磁性吸附,在滑动到一定程度后松手可以自动缩到顶端或自动拉伸到最大。

  先来看一个例子:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_abl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"> <android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" /> <android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabIndicatorColor="#FFFFFFFF"
app:tabIndicatorHeight="5.0dip"
app:tabSelectedTextColor="#FFFFFFFF"
app:tabTextColor="@color/colorAccent" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout>

  在这个例子中,AppBarLayout中嵌套了一个Toolbar和一个TabLayout,其中Toolbar中设置了layout_scrollFlags属性为“scroll|enterAlways”,因此Toolbar是可以根据滑动滑出屏幕;TabLayout没有设置layout_scrollFlags属性,因此它最终只会固定在屏幕上方。

  上面这个例子的运行结果如下图所示:

3、CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout:

  “CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout”的模式常用来制作Design模式的详情页页面,使用CollapsingToolbarLayout可以达到“视差模式”,即上滑过程中,顶部布局的消失是有一定的颜色或图案的变化效果的。

  CoordinatorLayout、AppBarLayout和CollapsingToolbarLayout这三个布局控件的嵌套顺序是这样的:CoordinatorLayout中嵌套AppBarLayout,AppBarLayout中嵌套CollapsingToolbarLayout,CollapsingToolbarLayout中嵌套一个Toolbar。

  CollapsingToolbarLayout中有一个title属性,可以设置CollapsingToolbarLayout上显示的标题文本,当CollapsingToolbarLayout中嵌套了Toolbar之后,滑动时这个文本最终会移动到Toolbar中。CollapsingToolbarLayout中还有一个contentScrim属性,用来设置Toolbar被折叠到顶部时显示的背景。

  CollapsingToolbarLayout为自己内部的子视图提供了一个属性:layout_collpaseMode,用来设置子视图的滑动模式,有两个值可以选择:

  • parallax:视差模式,在折叠过程中会有视差效果
  • pin:固定模式,此视图最后将固定在屏幕顶端

  我们来看一个例子:

<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_ctl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256.0dip"
android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="@color/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:title="aaaaaa"> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@mipmap/bg"
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" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout>

  在这个例子中,CollapsingToolbarLayout中嵌套了两个控件:一个ImageView和一个Toolbar,其中,ImageView设置了layout_collapseMode属性为parallax,表示它将在滑动过程中呈视差模式渐渐变化,最终滑出屏幕;而Toolbar设置的layout_collapseMode属性为pin,表示Toolbar会最终固定在屏幕上方,而且变成CollapsingToolbarLayout中contentScrim属性设置的背景样式,即Toolbar的背景会在最终变成colorPrimary颜色。

  这个例子的运行结果如下图所示:

      

  以上就是对CoordinatorLayout的基础用法的介绍,下面贴出码云中的源码,供大家参考。

DEMO地址

【Android - 控件】之MD - CoordinatorLayout的使用的更多相关文章

  1. Android控件大全(四)——CoordinatorLayout

    CoordinatorLayout 其实就是个高级的FrameLayout,用于协调子布局要使用该控件,需要再gradle中加入: compile 'com.android.support:desig ...

  2. RxJava RxBinding RxView 控件事件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  4. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  5. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  6. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

  7. Android 控件知识点,

    一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...

  8. UIAutomator定位Android控件的方法

    UIAutomator各种控件定位的方法. 1. 背景 使用SDK自带的NotePad应用,尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单 ...

  9. 从Android系统出发,分析Android控件构架

    从Android系统出发,分析Android控件构架 Android中所有的控件追溯到根源,就是View 和ViewGroup,相信这个大家都知道,但是大家也许会不太清楚它们之间的具体关系是什么,在A ...

  10. Android控件系列之RadioButton&RadioGroup(转)

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

随机推荐

  1. 聊一聊Java中的各种运算符

    计算机之所以叫“计算机”,其最基本用途之一就是运算,对应刚刚接触Java的小伙伴而言,熟悉并掌握Java中的各种运算符及其在表达式中的运算优先级是十分必要的. 算术运算 算术运算主要用来处理数学中的加 ...

  2. 面向对象的7个设计原则->开车理解->贴近生活

    设计模式在我们的开发中是不可或缺的一部分,很多人会说,我没用那些设计模式啊,我也开发的挺好的,其实不然,我们在开发中都用到了这些设计模式,只不过我们并没有在意这些,今天我就用开车的方法来解释一下我们的 ...

  3. textarea如何实现高度自适应(一)

    转自轩枫阁 - http://www.xuanfengge.com/textarea-on-how-to-achieve-a-high-degree-of-adaptive.html 方法一:div模 ...

  4. 阿里巴巴开源项目: 基于mysql数据库binlog的增量订阅&消费

    背景 早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数据库同步业务,主要是基于trigger的方式获取增量变更,不过从2010年开始,阿里系公司开始逐步的尝 ...

  5. ElasticSearch(六):索引模板

    ElasticSearch(六):索引模板 学习课程链接<Elasticsearch核心技术与实战> Index Template Index Template - 帮助你设定Mappin ...

  6. Apache的虚拟主机功能

    Apache的虚拟主机功能 (Virtual Host) 是可以让一台服务器基于IP.主机名或端口号实现提供多个网站服务的技术. 第一种情况:基于IP地址 这种情况很常见:一台服务器拥有多个IP地址, ...

  7. git 生成密匙时遇到报错 :Too many arguments.

    今天在我用新电脑想要向github上的远程库上传项目时,由于新电脑上的本地库没有与远程库建立连接,所以要在本地生成密匙添加到github上才可以上传,在我执行命令 ssh-keygen -t rsa- ...

  8. sql性能分析(explain关键字)

    explain关键字结果 列名所代表的的含义: Id:MySQL QueryOptimizer 选定的执行计划中查询的序列号.表示查询中执行 select 子句或操作表的顺序,id 值越大优先级越高, ...

  9. Docker学习-Kubernetes - 集群部署

    Docker学习 Docker学习-VMware Workstation 本地多台虚拟机互通,主机网络互通搭建 Docker学习-Docker搭建Consul集群 Docker学习-简单的私有Dock ...

  10. js在字符串中加入一段字符串

    在这个功能的实现主要是slice()方法的掌握 arrayObject.slice(start,end) start 必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说 ...