FloatingActionButton(FAB) 是 Android 5.0 新特性——Material Design 中的一个控件,是一种悬浮的按钮。

FloatingActionButton 是 ImageView 的子类,因此它具备ImageView的全部属性。

FloatingActionButton 结合 CoordinatorLayout 使用,即可实现悬浮在任意控件的任意位置。

使用 FloatingActionButton 的难点主要是布局,其在JAVA代码中的用法和普通的 ImageView 基本相同。

跟所有MD控件一样,要使用FAB,需要在gradle文件中先注册依赖:

  1. compile 'com.android.support:design:25.0.0'

1、FAB 基本属性:

  1. android:srcFAB中显示的图标,最好是24dp
  2. app:backgroundTint:正常的背景颜色
  3. app:rippleColor:按下时的背景颜色
  4. app:elevation:正常的阴影大小
  5. app:pressedTranslationZ:按下时的阴影大小
  6. app:layout_anchor:设置FAB的锚点,即以哪个控件为参照设置位置
  7. app:layout_anchorGravityFAB相对于锚点的位置
  8. app:fabSizeFAB的大小,normalmini(对应56dp40dp
  9. 注意:要想让FAB显示点击后的颜色和阴影变化效果,必须设置onClick事件

实例布局代码:

  1. <android.support.design.widget.CoordinatorLayout
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <android.support.design.widget.FloatingActionButton
  7. android:id="@+id/fab"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_margin="20.0dip"
  11. android:onClick="click"
  12. android:src="@mipmap/ic_launcher"
  13. app:backgroundTint="#30469b"
  14. app:borderWidth="0.0dip"
  15. app:elevation="5.0dip"
  16. app:fabSize="normal"
  17. app:layout_anchor="@id/container"
  18. app:layout_anchorGravity="right|bottom"
  19. app:pressedTranslationZ="10.0dip"
  20. app:rippleColor="#a6a6a6" />
  21.  
  22. </android.support.design.widget.CoordinatorLayout>

运行结果图:

2、交互事件:

FloatingActionButton 的用法和ImageView基本相同,要想设置FloatingActionButton的点击事件,直接调用setOnClickListener()方法即可。

值得一提的是,当FloatingActionButton与Snackbar一起使用的时候,有时候会发生“Snackbar将FloatingActionButton覆盖”的问题,即FloatingActionButton不会因为Snackbar的弹出而上移,这是因为Snackbar的make()方法第一个参数没有与FloatingActionButton绑定,只要Snackbar的make()方法第一个参数是FloatingActionButton对象,就不会出现上述问题,实例代码如下:

  1. fab.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // 第一个参数设置为FAB就可以使FAB跟随Snackbar移动
  5. Snackbar.make(v, "去下一页?", Snackbar.LENGTH_LONG).setAction("确定", new View.OnClickListener() {
  6. @Override
  7. public void onClick(View v) {
  8. startActivity(new Intent(MainActivity.this, NextActivity.class));
  9. }
  10. }).show();
  11. }
  12. });

运行结果如下图:

       

3、FAB 滑动隐藏/显示:

很多应用中都有这种界面:界面中有一个FAB和一个RecyclerView,当RecyclerView向下滑动时,FAB消失;当RecyclerView向上滑动时,FAB又显示出来。这是FAB与RecyclerView、CoordinatorLayout结合使用的结果。通过设置FAB在CoordinatorLayout中的Behavior来实现这种效果。下面贴代码:

布局代码:

  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:id="@+id/activity_next"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6.  
  7. <android.support.v7.widget.RecyclerView
  8. android:id="@+id/next_rv"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" />
  11.  
  12. <android.support.design.widget.FloatingActionButton
  13. android:id="@+id/next_fab"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_margin="20.0dip"
  17. android:onClick="click"
  18. android:src="@mipmap/ic_launcher"
  19. app:backgroundTint="@color/colorPrimaryDark"
  20. app:elevation="5.0dip"
  21. app:fabSize="normal"
  22. app:layout_anchor="@id/next_rv"
  23. app:layout_anchorGravity="bottom|right"
  24. app:layout_behavior="com.example.testfloatingactionbutton.ScrollAwareFABBehavior"
  25. app:pressedTranslationZ="10.0dip"
  26. app:rippleColor="@color/colorPrimary" />
  27.  
  28. </android.support.design.widget.CoordinatorLayout>

ScrollAwareFABBehavior类中的代码:

  1. public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
  2. // 动画插值器,可以控制动画的变化率
  3. private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
  4.  
  5. // 是否正在执行隐藏的动画
  6. private boolean mIsAnimatingOut = false;
  7.  
  8. public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
  9. super();
  10. }
  11.  
  12. @Override
  13. public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
  14. final View directTargetChild, final View target, final int nestedScrollAxes) {
  15. // 如果是上下滑动的,则返回true
  16. return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
  17. || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
  18. }
  19.  
  20. @Override
  21. public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
  22. final View target, final int dxConsumed, final int dyConsumed,
  23. final int dxUnconsumed, final int dyUnconsumed) {
  24. super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
  25. if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
  26. // 用户向下滑动时判断是否正在执行隐藏动画,如果不是,而且FAB当前是可见的,则执行隐藏动画隐藏FAB
  27. animateOut(child);
  28. } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
  29. // 用户向上滑动时判断FAB是否可见,如果不可见则执行显示动画显示FAB
  30. animateIn(child);
  31. }
  32. }
  33.  
  34. // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
  35. // 执行隐藏动画隐藏FAB
  36. private void animateOut(final FloatingActionButton button) {
  37. if (Build.VERSION.SDK_INT >= 14) {
  38. ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer()
  39. .setListener(new ViewPropertyAnimatorListener() {
  40. public void onAnimationStart(View view) {
  41. ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
  42. }
  43.  
  44. public void onAnimationCancel(View view) {
  45. ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
  46. }
  47.  
  48. public void onAnimationEnd(View view) {
  49. ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
  50. view.setVisibility(View.GONE);
  51. }
  52. }).start();
  53. } else {
  54. Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_out);
  55. anim.setInterpolator(INTERPOLATOR);
  56. anim.setDuration(200L);
  57. anim.setAnimationListener(new Animation.AnimationListener() {
  58. public void onAnimationStart(Animation animation) {
  59. ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
  60. }
  61.  
  62. public void onAnimationEnd(Animation animation) {
  63. ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
  64. button.setVisibility(View.GONE);
  65. }
  66.  
  67. @Override
  68. public void onAnimationRepeat(final Animation animation) {
  69. }
  70. });
  71. button.startAnimation(anim);
  72. }
  73. }
  74.  
  75. // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
  76. // 执行显示动画显示FAB
  77. private void animateIn(FloatingActionButton button) {
  78. button.setVisibility(View.VISIBLE);
  79. if (Build.VERSION.SDK_INT >= 14) {
  80. ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
  81. .setInterpolator(INTERPOLATOR).withLayer().setListener(null)
  82. .start();
  83. } else {
  84. Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_in);
  85. anim.setDuration(200L);
  86. anim.setInterpolator(INTERPOLATOR);
  87. button.startAnimation(anim);
  88. }
  89. }
  90. }

FAB显示的动画fab_in:

  1. <set xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/decelerate_interpolator"
  3. android:zAdjustment="top">
  4. <scale
  5. android:duration="@android:integer/config_mediumAnimTime"
  6. android:fromXScale="1.0"
  7. android:fromYScale="1.0"
  8. android:pivotX="50%p"
  9. android:pivotY="50%p"
  10. android:toXScale=".5"
  11. android:toYScale=".5" />
  12. <alpha
  13. android:duration="@android:integer/config_mediumAnimTime"
  14. android:fromAlpha="1.0"
  15. android:toAlpha="0" />
  16. </set>

FAB隐藏的动画fab_out:

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:interpolator="@android:anim/decelerate_interpolator">
  4. <scale
  5. android:duration="@android:integer/config_mediumAnimTime"
  6. android:fromXScale="2.0"
  7. android:fromYScale="2.0"
  8. android:pivotX="50%p"
  9. android:pivotY="50%p"
  10. android:toXScale="1.0"
  11. android:toYScale="1.0" />
  12. </set>
  13. </set>

运行效果如下图:

以上就是对FloatingActionButton用法的简单介绍,下面贴出码云上的代码,供大家参考。

DEMO地址

【Android - MD】之FloatingActionButton的使用的更多相关文章

  1. 【Android - MD】之CoordinatorLayout的使用

    CoordinatorLayout是Android 5.0新特性--Material Design中的一个布局控件,主要用来协调各个子视图之间的工作,也可以用来作为顶部布局.CoordinatorLa ...

  2. 【Android - MD】之Snackbar的使用

    Snackbar 是 Android 5.0 新特性--Material Design 中的一个控件,用来代替 Toast ,Snackbar与Toast的主要区别是:Snackbar可以滑动退出,也 ...

  3. 【Android - MD】之NavigationView的使用

    NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...

  4. 【Android - MD】之TextInputLayout的使用

    TextInputLayout是Android 5.0新特性--Material Design中的一个布局控件,主要用来嵌套EditText,实现数据输入时的一些效果,如: 当输入框获取焦点时,输入提 ...

  5. 【Android - MD】之TabLayout的使用

    TabLayout是Android 5.0新特性--Material Design中的一个控件,是一个标签页的导航条,常结合ViewPager完成页面导航. 和其他MD控件一样,使用TabLayout ...

  6. Android——MaterialDesign之四 FloatingActionButton、Snackbar、CoordinaterLayout

    FloatingActionButton 悬浮按钮,默认colorAccent来作为按钮的颜色 <android.support.design.widget.FloatingActionButt ...

  7. 【Android - MD】之RecyclerView的使用

    RecyclerView是Android 5.0新特性--Material Design中的一个控件,它将ListView.GridView整合到一起,可以使用极少的代码在ListView.GridV ...

  8. 【Android - MD】之CardView的使用

    CardView是Android 5.0新特性--Material Design中的一个布局控件,可以通过属性设置显示一个圆角的类似卡片的视图. 1.CardView的属性: app:cardCorn ...

  9. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout

    如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...

随机推荐

  1. Gtest打桩函数

    假设Client的定义如下 class Client { ...... public: virtual bool GetData(std::string& data); ...... }; 我 ...

  2. C#连接、访问MySQL数据库

    一.准备工具 visual stuido(本示例使用visual studio 2010) MySql.Data.dll mysql_installer_community_V5.6.21.1_set ...

  3. 使用 fn 标签 解决字数过多时用省略号代替 .............................

    list列表单条记录某字段大于10就后面添加省略号(如:内容只是显示开始的10个字,后面的用省略号代替) 在list列表中单条记录某字段大于10就后面添加省略号, 首先引入 jstl标签: <% ...

  4. mysql-积累管理sql语句

    //连接数据库 mysql -h xxx -u root -p; //查看数据库 show databases //查看数据表 show tables //查看某数据表结构 desc xxx表 //修 ...

  5. VMWare10安装Ubuntu 13.10过程

    把这当成自己的第一篇文章吧,准备工作环境切换到linux,选择Ubuntu 13.10桌面版,Ubuntu官网下载 先安装VMWare10,这个没什么可说的,安装好后启动,点击"新建虚拟机& ...

  6. Python3.4使用MySql

    如何在Django1.6结合Python3.4版本中使用MySql django默认的mysql连接是Mysqldb,悲催的是此版本只支持到python2.7,oracle官方的mysql-conne ...

  7. vs2015安装没有wim32

    刚开始在官网上下载VS2015没在意太多,选择了默认安装,结果是没有win64的,所以就不能写c代码.默认安装很多库都没有,所以要什么都得下载.转载一篇文章

  8. 使用spm build 批量打包压缩seajs 代码

    一,安装环境 1.安装spm spm工具是基于node(nodejs的服务平台)的,因此我们需要先安装 node 和 npm 下载地址:http://nodejs.org/#download.下载完成 ...

  9. Phonegap 3.0 拍照 出错的说明

    在官方3.0 提供的摄像机操作例子是不成功的,因为该例子没有说明摄像机操作需要添加Plugin. 添加插件方法(安装cordova3.0时必须使用官方命令行方式,通过nodejs安装,且装上了git) ...

  10. HTTP-304 NOT Modified

    http://www.douban.com/note/161120791/ http://blog.sina.com.cn/s/blog_4c98b9600100jd4z.html http://ww ...