Android CoordinatorLayout 入门介绍

在 2015 年的 I/O 开发者大会上,Google 介绍了一个新的 Android Design Support Library,该库可以帮助开发者在应用上使用 meterial design。它包含了许多重要的 meterial design 的构建块,并且它支持 API 7及以上的版本。如果你错过了这次大会,那就请打开谷歌开发者站点来查阅它的相关信息吧:传送门

CoordinatorLayout


在库中所有好的东东之间,看起来真正有趣的是 CoordinatorLayout,它是一个 FrameLayout。从它的名字中,你或许已经猜到:该布局的强大在于,能够协调子元素之间的依赖关系。

你需要做的就是把 View 包含在 CoordinatorLayout 中。让我们直接进入代码吧。下面的例子非常简单,它包含了 Floating Action Button,点击时会触发一个 Snackbar

首先,在 gradle 文件中引入 meterial design 库:

compile 'com.android.support:design:22.2.0'

接下来,为 Activity 创建一个简单的布局文件:

  

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/ic_done" /> </android.support.design.widget.CoordinatorLayout>

  

  还需要添加 MainActivity

  

public class MainActivity extends AppCompatActivity { 

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hello Snackbar", Snackbar.LENGTH_LONG).show();
}
});
}
}

  

效果显示
 

非常酷,是吗?

但是,如果你想使用其它 FAB 的实现又会怎么样呢?因为 Support Library 中的 FAB 实现没有菜单选项,于是,我们尝试一下由开发者 Base 实现的 FAB 开源库。

compile 'com.getbase:floatingactionbutton:1.9.1'

  

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:fab_icon="@drawable/ic_done" /> </android.support.design.widget.CoordinatorLayout>

  

开源库效果显示
 

如图所示,在这种情况下,CoordinatorLayout 不能正常工作。这是因为 View 没有默认的CoordinatorLayout.Behavior 的实现。现有的解决方案就是等待有人来完善它。

或者呢,我们可以为这个组件写一个自定义的行为实现。:D

View 知道如何表现


这个框架真的非常强大,要给 view 自定义行为时,你根本不需要获取这个 view。你还可以改变任意 view 的默认行为。

首先,需要继承 Behavior 类:

public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton>

为了使这个类可以填充 xml 中的内容,我们需要给它设置一个构造方法,方法有两个参数:Context 和AttributeSet

public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {}

接下来的步骤是重写 layoutDependsOn() 方法,并且,如果我们想监听改变,就让方法返回 true。在例子中,我们只想监听 Snackbar 对象的改变。

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return dependency instanceof SnackbarLayout;
}

 

现在,让我们继承真正行为的实现。当 CoordinatorLayout 中的 view 每次发生变化时,onDependentViewChanged 方法都会被调用。在这个方法中,我们要读取当前 Snackbar 的状态。当Snackbar 显示的时候,我们想把 FAB 也移上来。要实现这样的目的,我们需要把 FAB 的 Y 坐标设置为Snackbar 的高度。要得到正确的转换值,我们需要从转化的 Y 值中减去 Snackbar 的高度。

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}

  

最后一步就是告诉 CoordinatorLayout 使用 FloatingActionButtonBeahvior

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:layout_behavior="com.getbase.coordinatorlayoutdemo.FloatingActionButtonBehavior"
app:fab_icon="@drawable/ic_done" /> </android.support.design.widget.CoordinatorLayout>

  

最终,问题被修正了:

问题修正后的效果显示
 

如果你想为 view 定义默认行为,只需要在你的 Behavior 类上添加 DefaultBehavior 注解就可以了。

以上代码的 GitHub 地址为:https://github.com/ggajews/coordinatorlayoutwithfabdemo 。

祝大家编码愉快!

Android CoordinatorLayout 入门介绍的更多相关文章

  1. [原]Android Fragment 入门介绍

    Fragment Fragment 产生,优点,用途,使用方法简介 1 Fragmeng简介 自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片.片段.其上的是为 ...

  2. android surfaceview 入门介绍

    由于工作中需自定义控件,以前没写过. 开始时,实用view 实现了,经理说不好,担心效率低,要求每秒需要刷新10次左右. 然后,学习使用  surfaceview. 看了网上简单的Demo,找到him ...

  3. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解

    原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...

  4. [译]:Xamarin.Android开发入门——Hello,Android深入理解

    返回索引目录 原文链接:Hello, Android_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android深入理解 本部分介绍利用Xamarin开发And ...

  5. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  6. Hello, Android 快速入门

    Hello, Android Android 开发与 Xamarin 简介 在这两节指南中,我们将 (使用 Xamarin Studio或 Visual Studio)建立我们的第一个 Xamarin ...

  7. Android编译系统入门(二)

    Android.mk的使用方法 在上一篇Android编译系统入门(一)中我们只要介绍了Android系统使用make命令默认编译的依赖树是droid,而droid是一个伪目标,它有两个先决条件dro ...

  8. Android开发入门要点记录:四大组件

    cocos2dx跨平台开发中需要了解android开发,昨天快速的浏览了一本Android开发入门教程,因为之前也似懂非懂的写过Activity,Intent,XML文件,还有里面许多控件甚至编程思想 ...

  9. 《Delphi XE6 android 编程入门教程》推荐

    近5.6年已经没有看见关于delphi的新技术的书出来了(看来在国内delphi的使用量确实很低了), 高勇同学最近出了一本<Delphi XE6 android 编程入门教程>,上周刚拿 ...

随机推荐

  1. 【转载】【Todo】银弹与我们的职业

    看到一段文字,不得不单独拎出来. 然后再借用一下g9老大的<银弹和我们的职业>中的话: 银弹和我们的职业发展有什么相干?很简单:我们得把时间用于学习解决本质困难.新技术给高手带来方便.菜鸟 ...

  2. Django 工作流程

    一.Django 工作流程 在开始具体的代码之旅前,先来宏观地看下Django是如何处理Http Resquest的,如下图: 假设你已经在浏览器输入了 http://127.0.0.1:8000/p ...

  3. ci框架(一)

    ci目录结构                                                                                    |-----syst ...

  4. [TypeScript] Work with DOM Elements in TypeScript using Type Assertions

    The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...

  5. 【ACM】Crixalis's Equipment

      #include "stdio.h" #include "stdlib.h" /* 贪心算法: Ai->x 表示第i个物品的体积 Ai->y 表 ...

  6. SQLServer:探讨EXEC与sp_executesql的区别详解

    摘要 MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的 ...

  7. Bitmap和Drawable的互相转换

    刚好之前的项目实用到.怕遗忘了.就先记录下来.然后会用到的时候直接来这copy使用就好了. 1.Bitmap ---->Drawable: public static Drawable bitm ...

  8. Vim快捷键整理

    Vim主要分为两种模式一种是Insert模式,该模式下可以像其它文本编辑器一样正常输入字符:另一种是Normal模式,该模式下Vim监听用户的按键可以对文本进行快速修改. 想要从Insert模式切换到 ...

  9. 【DB2】查询上月末、上年末、上年同期等信息

    此处以20180612为例子 想得到上年末.上年同期.上月末这些时间点,只需要记住函数ADD_MONTHS.LAST_DAY.ADD_YEARS这些函数即可. 上年末 SELECT SUBSTR(TO ...

  10. struts2 result type类型

    result标签中type的类型 类型 说明 chain 用于Action链式处理 dispatcher 用于整合JSP,是<result>元素默认的类型 freemarket 用来整合F ...