FloatingActionButton 完全解析[Design Support Library(2)]
一、简单使用
布局:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="@drawable/ic_discuss"
/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
使用非常简单,直接当成ImageView来用即可。
效果图:
可以看到我们的FloatingActionButton正常显示的情况下有个填充的颜色,有个阴影;点击的时候会有一个rippleColor,并且阴影的范围可以增大,那么问题来了:
这个填充色以及rippleColor如何自定义呢?
默认的颜色取的是,theme中的colorAccent,所以你可以在style中定义colorAccent。
colorAccent 对应EditText编辑时、RadioButton选中、CheckBox等选中时的颜色。详细请参考:Android 5.x Theme 与 ToolBar 实战
rippleColor默认取的是theme中的colorControlHighlight。
我们也可以直接用过属性定义这两个的颜色:
app:backgroundTint="#ff87ffeb"
app:rippleColor="#33728dff"- 1
- 2
立体感有没有什么属性可以动态指定?
和立体感相关有两个属性,elevation和pressedTranslationZ,前者用户设置正常显示的阴影大小;后者是点击时显示的阴影大小。大家可以自己设置尝试下。
综上,如果你希望自定义颜色、以及阴影大小,可以按照如下的方式(当然,颜色你也可以在theme中设置):
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="@drawable/ic_discuss"
app:backgroundTint="#ff87ffeb"
app:rippleColor="#33728dff"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
至于点击事件,和View的点击事件一致就不说了~~
二、5.x存在的一些问题
在5.x的设备上运行,你会发现一些问题(测试系统5.0):
- 木有阴影
记得设置app:borderWidth="0dp"。
- 按上述设置后,阴影出现了,但是竟然有矩形的边界(未设置margin时,可以看出)
需要设置一个margin的值。在5.0之前,会默认就有一个外边距(不过并非是margin,只是效果相同)。
so,良好的实践是:
- 添加属性
app:borderWidth="0dp" - 对于5.x设置一个合理的margin
如下:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
app:borderWidth="0dp"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_headset" />
- 1
- 2
- 3
- 4
- 5
- 6
- 7
values
<dimen name="fab_margin">0dp</dimen>
- 1
values-v21
<dimen name="fab_margin">16dp</dimen>
FloatingActionButton 完全解析[Design Support Library(2)]的更多相关文章
- Android Design Support Library: 学习CoordinatorLayout
简述 CoordinatorLayout字面意思是"协调器布局",它是Design Support Library中提供的一个超级帧布局,帮助我们实现Material Design ...
- Android Design Support Library(三)用CoordinatorLayout实现Toolbar隐藏和折叠
此文的代码在Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果代码的基础上进行修改,如果你没有看过本系列的第一篇文章最好先看一看.Co ...
- Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...
- Android开发学习之路-Android Design Support Library使用(CoordinatorLayout的使用)
效果图: 上面的这个图有两个效果是,一个是顶部的图片,在上滑之后会隐藏起来并且显示出一个ToolBar(ToolBar类似于ActionBar,但是只有ToolBar是兼容Material Desig ...
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- 【转】Android的材料设计兼容库(Design Support Library)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/developer/2015/0531/2958.html?mType=Group Android的材料设计兼容 ...
- Android应用Design Support Library完全使用实例
阅读目录 2-1 综述 2-2 TextInputLayout控件 2-3 FloatingActionButton控件 2-4 Snackbar控件 2-5 TabLayout控件 2-6 Navi ...
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
- Codelab for Android Design Support Library used in I/O Rewind Bangkok session
At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...
随机推荐
- Python-----格式化字符
摘要: Python中 %s . %r Python中也有类似于C中的 printf()格式输出,使用 % 运算符,格式: 格式标记字符串 % 要输出的值组 右边的”值组“若有两个及以上的值则需要用小 ...
- 灯塔(LightHouse)
Description As shown in the following figure, If another lighthouse is in gray area, they can beacon ...
- 完美解决ListView 与 ScrollView 共存问题
1:首先设置ListView的高度,在setAdapter之后调用此方法. public static void setListViewHeightBasedOnChildren(ListView l ...
- PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输
前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...
- InnoSetup中枚举出INI文件的所有sections和键值
原文 http://blog.chinaunix.net/uid-23480430-id-3016899.html InnoSetup支持一些INI文件操作函数, 例如GetIniString,Ini ...
- inux关于readlink函数获取运行路径的小程序
inux关于readlink函数获取运行路径的小程序 相关函数: stat, lstat, symlink 表头文件: #include <unistd.h> 定义函数:int re ...
- windows phone中三种解析XML的方法
需求如下, 项目需要将一段xml字符串中的信息提取出来 <?xml version=""1.0"" encoding=""UTF-8& ...
- back_insert_iterator和iterator用起来不一样。
先看代码: #include<iostream> #include<vector> #include<algorithm> #include<iterator ...
- softlayer virtual machine vhd磁盘镜像导入shell脚本
脚本
- iOS开发CoreAnimation解读之二——对CALayer的分析
iOS开发CoreAnimation解读之二——对CALayer的分析 一.UIView中的CALayer属性 1.Layer专门负责view的视图渲染 2.自定义view默认layer属性的类 二. ...