Android开发悬浮按钮 Floating ActionButton的实现方法
作者:听着music睡 时间:2023-05-02 10:44:07
一、介绍
这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性
android.support.design.widget.FloatingActionButton
二、使用准备, 在as 的 build.grade文件中写上
compile 'com.android.support:design:22.2.0'
三、使用说明
xml文件中,注意蓝色字体部分
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerInParent="true"
android:src="@mipmap/ok"
app:borderWidth="0dp"
app:backgroundTint="#FF4011"
app:rippleColor="#33728dff"
app:elevation="8dp"
app:pressedTranslationZ="16dp"
/>
可以看到我们使用了app属性,则需要在根容器中添加这个属性。
xmlns:app="http://schemas.android.com/apk/res-auto"属性介绍:
1、app:borderWidth=""------------------边框宽度,通常设置为0 ,用于解决Android 5.X设备上阴影无法正常显示的问题
2、app:backgroundTint=""---------------按钮的背景颜色,不设置,默认使用theme中colorAccent的颜色
3、app:rippleColor=""--------------------点击的边缘阴影颜色
4、app:elevation=""----------------------边缘阴影的宽度
5、app:pressedTranslationZ="16dp"-----点击按钮时,按钮边缘阴影的宽度,通常设置比elevation的数值大
另外我们希望点击按钮一个颜色,正常状态一个颜色 以提高用户体验,那么就是drawable文件夹中创建文件 floatbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorNormal"></item>
<item android:state_pressed="true" android:drawable="@color/colorPressed"></item>
</selector>
然后我们设置FloatActionButton的backgroud为这个drawable文件即可,同时app:backgroundTint=""属性也可以不再设置
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/floatbutton"
android:src="@mipmap/ok"
app:borderWidth="0dp"
app:rippleColor="#33728dff"
app:elevation="8dp"
app:pressedTranslationZ="16dp"
/>
效果图:
来源:http://www.cnblogs.com/xqxacm/archive/2016/09/08/5852783.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android如何实现扫描和生成二维码
![](https://img.aspxhome.com/file/2023/3/137563_0s.jpg)
Java导出Execl疑难点处理的实现
![](https://img.aspxhome.com/file/2023/3/80293_0s.png)
实例代码讲解JAVA 观察者模式
![](https://img.aspxhome.com/file/2023/9/83139_0s.jpg)
Java中遍历Map集合的5种方式总结
Java泛型的类型擦除示例详解
![](https://img.aspxhome.com/file/2023/0/78330_0s.png)
Java二维数组实现数字拼图效果
浅谈Java8 判空新写法
![](https://img.aspxhome.com/file/2023/2/75712_0s.jpg)
JAVA Integer类常用方法解析
Mybatis源码解析之事务管理
![](https://img.aspxhome.com/file/2023/5/101975_0s.png)
Java枚举类与注解,新手一篇搞定它
![](https://img.aspxhome.com/file/2023/8/68148_0s.png)
每日六道java新手入门面试题,通往自由的道路
Spring MVC文件配置以及参数传递示例详解
![](https://img.aspxhome.com/file/2023/0/108460_0s.png)
Java Set集合的遍历及实现类的比较
Android7.0自动更新适配 包解析异常
Android读取手机通讯录联系人到自己项目
C#微信公众平台开发之access_token的获取存储与更新
![](https://img.aspxhome.com/file/2023/8/111228_0s.jpg)
ThreadLocal使用案例_动力节点Java学院整理
Log4j新手快速入门教程
![](https://img.aspxhome.com/file/2023/0/91740_0s.png)
C#使用第三方组件实现动态解析和求值字符串表达式
![](https://img.aspxhome.com/file/2023/0/104370_0s.jpg)