FloatingActionButton的使用

来源:互联网 发布:大数据技术方向 编辑:程序博客网 时间:2024/05/16 10:27

FloatingActionButton是继承至ImageView,所以FloatingActionButton拥有ImageView的所有属性。CoordinatorLayout可以用来配合FloatingActionButton浮动按钮,设置app:layout_anchor和app:layout_anchorGravity构建出特定的位置与效果的FloatingActionButton。

我们来看看怎么使用FloatingActionButton吧:

[html] view plain copy
 print?
  1. <android.support.design.widget.FloatingActionButton  
  2.         android:id="@+id/fab"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_margin="16dp"  
  6.         android:src="@mipmap/icon"  
  7.         app:backgroundTint="#30469b"  
  8.         app:borderWidth="0dp"  
  9.         app:elevation="6dp"  
  10.         app:fabSize="normal"  
  11.         app:layout_anchor="@id/coordinator_layout"  
  12.         app:layout_anchorGravity="bottom|right"  
  13.         app:pressedTranslationZ="12dp"  
  14.         app:rippleColor="#a6a6a6" />  
各个属性的意思:

  • app:backgroundTint - 设置FAB的背景颜色。
  • app:rippleColor - 设置FAB点击时的背景颜色。
  • app:borderWidth - 该属性尤为重要,如果不设置0dp,那么在4.1的sdk上FAB会显示为正方形,而且在5.0以后的sdk没有阴影效果。所以设置为borderWidth="0dp"。
  • app:elevation - 默认状态下FAB的阴影大小。
  • app:pressedTranslationZ - 点击时候FAB的阴影大小。
  • app:fabSize - 设置FAB的大小,该属性有两个值,分别为normal和mini,对应的FAB大小分别为56dp和40dp。
  • src - 设置FAB的图标,Google建议符合Design设计的该图标大小为24dp。
  • app:layout_anchor - 设置FAB的锚点,即以哪个控件为参照点设置位置。
  • app:layout_anchorGravity - 设置FAB相对锚点的位置,值有 bottom、center、right、left、top等。
这样设置后,就可以在屏幕右下角创建出一个FloatingActionButton了。如:


一般情况下,FAB与Snackbar配合使用时候会出现Snackbar遮住FAB:如:


为了解决这个问题,我们把Snackbar.make(View view,,).show();的第一个参数View设置为CoordinatorLayout,即:

[java] view plain copy
 print?
  1. //把mCoordinatorLayout传给Snackbar  
  2. Snackbar.make(mCoordinatorLayout, "Snackbar", Snackbar.LENGTH_SHORT).show();  

这样CoordinatorLayout就可以协调各个View之间的动画效果,效果就变为了:


即Snackbar不会遮挡FAB的显示了,当Snackbar出现时FAB会自动上移。

当然FAB的点击事件也是通过setOnClickListener()设置即可。


我们再看一个效果:


这个效果其实就是通过改变FAB的Layout_anchor和layout_anchorGravity来实现的,看看布局就明白了:

[html] view plain copy
 print?
  1. <android.support.design.widget.CoordinatorLayout   
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:id="@+id/coordinator_layout"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     tools:context=".MainActivity">  
  9.   
  10.     <android.support.design.widget.AppBarLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="256dp"  
  13.         android:fitsSystemWindows="true">  
  14.   
  15.         <android.support.design.widget.CollapsingToolbarLayout  
  16.             android:id="@+id/collapsingToolbarLayout"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="match_parent"  
  19.             app:contentScrim="#30469b"  
  20.             app:expandedTitleMarginStart="48dp"  
  21.             app:layout_scrollFlags="scroll|exitUntilCollapsed">  
  22.   
  23.             <ImageView  
  24.                 android:layout_width="match_parent"  
  25.                 android:layout_height="match_parent"  
  26.                 android:scaleType="centerCrop"  
  27.                 android:src="@mipmap/bg"  
  28.                 app:layout_collapseMode="parallax"  
  29.                 app:layout_collapseParallaxMultiplier="0.7" />  
  30.   
  31.             <android.support.v7.widget.Toolbar  
  32.                 android:id="@+id/toolbar"  
  33.                 android:layout_width="match_parent"  
  34.                 android:layout_height="?attr/actionBarSize"  
  35.                 app:layout_collapseMode="pin" />  
  36.         </android.support.design.widget.CollapsingToolbarLayout>  
  37.     </android.support.design.widget.AppBarLayout>  
  38.     <android.support.design.widget.FloatingActionButton  
  39.         android:id="@+id/fab"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:src="@mipmap/icon"  
  43.         app:backgroundTint="#30469b"  
  44.         app:borderWidth="0dp"  
  45.         app:elevation="6dp"  
  46.         app:fabSize="normal"  
  47.         app:layout_anchor="@id/collapsingToolbarLayout"  
  48.         app:layout_anchorGravity="bottom|center"  
  49.         app:pressedTranslationZ="12dp"  
  50.         app:rippleColor="#a6a6a6" />  
  51.     <android.support.v7.widget.RecyclerView  
  52.         android:id="@+id/recyclerView"  
  53.         android:layout_width="match_parent"  
  54.         android:layout_height="match_parent"  
  55.         android:scrollbars="none"  
  56.         app:layout_behavior="@string/appbar_scrolling_view_behavior" />  
  57. </android.support.design.widget.CoordinatorLayout>  
转自:http://blog.csdn.net/u010687392/article/details/46954213      (Sunzxyong
源码地址:http://download.csdn.net/detail/u010687392/8913513


0 0
原创粉丝点击