FloatingActionButton 完全解析[Design Support Library(2)]
2015-12-02 14:40
399 查看
一、简单使用
布局:<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。
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>
相关文章推荐
- Delphi与SQL模糊like通配符查询(转载)
- leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)
- scrapy 模拟登录后再抓取
- C语言中sizeof()与strlen()函数的使用入门及对比
- 父子进程ID和进程收养问题(init与upstart)
- Struts2基础
- 杭电acm1228
- MySQL Study之--Linux下安装MySQL workbench工具
- 数据库sql中in和exists的区别
- Android Studio 快捷键
- 如何反编译APK包?
- [转]让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
- [业务]水资源做什么?
- jersey
- Asp.Net 之 Web.config下Authorization节点
- AsyncTask和Handler的优缺点比较
- Spring注解
- C# 快速创建线程
- Android SwipeRefreshLayout 官方下拉刷新控件介绍
- 利用UIImageJPEGRepresentation与UIGraphicsBeginImageContext进行图片压缩的简单比较