FloatingActionButton 完全解析 Design Support Library 2

2023-11-07 18:48

本文主要是介绍FloatingActionButton 完全解析 Design Support Library 2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

       

FloatingActionButton 完全解析[Design Support Library(2)]

转载请标明出处:
  [http://blog.csdn.net/lmj623565791/article/details/46678867](http://blog.csdn.net/lmj623565791/article/details/46678867;
  本文出自:【张鸿洋的博客】

哈,跟随着上篇Android 自己实现 NavigationView [Design Support Library(1)]之后,下面介绍个Design Support Library中极其简单的控件:FloatingActionButton

一、简单使用

布局:

        <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>
  • 1

三、简单实现FloatActionButton

参考参考资料4

可以通过drawable来实现一个简单的阴影效果:

drawable/fab.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <layer-list>            <!-- Shadow -->            <item android:top="1dp" android:right="1dp">                <layer-list>                    <item>                        <shape android:shape="oval">                            <solid android:color="#08000000"/>                            <padding                                android:bottom="3px"                                android:left="3px"                                android:right="3px"                                android:top="3px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#09000000"/>                            <padding                                android:bottom="2px"                                android:left="2px"                                android:right="2px"                                android:top="2px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#10000000"/>                            <padding                                android:bottom="2px"                                android:left="2px"                                android:right="2px"                                android:top="2px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#11000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#12000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#13000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#14000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#15000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#16000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                </layer-list>            </item>            <!-- Blue button pressed -->            <item>                <shape android:shape="oval">                    <solid android:color="#90CAF9"/>                </shape>            </item>        </layer-list>    </item>    <item android:state_enabled="true">        <layer-list>            <!-- Shadow -->            <item android:top="2dp" android:right="1dp">                <layer-list>                    <item>                        <shape android:shape="oval">                            <solid android:color="#08000000"/>                            <padding                                android:bottom="4px"                                android:left="4px"                                android:right="4px"                                android:top="4px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#09000000"/>                            <padding                                android:bottom="2px"                                android:left="2px"                                android:right="2px"                                android:top="2px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#10000000"/>                            <padding                                android:bottom="2px"                                android:left="2px"                                android:right="2px"                                android:top="2px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#11000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#12000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#13000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#14000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#15000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                    <item>                        <shape android:shape="oval">                            <solid android:color="#16000000"/>                            <padding                                android:bottom="1px"                                android:left="1px"                                android:right="1px"                                android:top="1px"                                />                        </shape>                    </item>                </layer-list>            </item>            <!-- Blue button -->            <item>                <shape android:shape="oval">                    <solid android:color="#03A9F4"/>                </shape>            </item>        </layer-list>    </item></selector>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240

一个相当长的drawable,不过并不复杂,也比较好记忆。首先为一个View添加阴影,使用的是color从#08000000#1500000,取的padding值为4、2、2、1…1;这样就可以为一个View的四边都添加上阴影效果。

当然了,为了阴影更加逼真,把上述的Layer-list又包含到了一个item中,并为该item设置了top和right,为了让左,下的阴影效果比上、右重,当然你可以设置其他两边,改变效果。

最后添加一个item设置为按钮的填充色(注意该item的层级)。

该drawable为一个selector,所以press和默认状态写了两次基本一致的代码,除了填充色不同。

使用:

 <ImageButton        android:layout_width="56dp"        android:layout_height="56dp"        android:layout_gravity="bottom|right"        android:layout_margin="20dp"        android:background="@drawable/fab"        android:src="@drawable/ic_done"        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

效果图:

ok,到此FloatActionButton就介绍完毕啦~~有兴趣的话,也可以从github挑选个库看看别人的实现。

ok~

新浪微博
  微信公众号:hongyangAndroid
  (欢迎关注,第一时间推送博文信息)
 

参考资料

  • http://antonioleiva.com/floating-action-button/
  • https://code.google.com/p/android/issues/detail?id=175067
  • https://github.com/chrisbanes/cheesesquare
  • http://www.myandroidsolutions.com/2015/01/01/android-floating-action-button-fab-tutorial/
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述

这篇关于FloatingActionButton 完全解析 Design Support Library 2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/365581

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

HDU 2159 二维完全背包

FATE 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

OWASP十大安全漏洞解析

OWASP(开放式Web应用程序安全项目)发布的“十大安全漏洞”列表是Web应用程序安全领域的权威指南,它总结了Web应用程序中最常见、最危险的安全隐患。以下是对OWASP十大安全漏洞的详细解析: 1. 注入漏洞(Injection) 描述:攻击者通过在应用程序的输入数据中插入恶意代码,从而控制应用程序的行为。常见的注入类型包括SQL注入、OS命令注入、LDAP注入等。 影响:可能导致数据泄

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

多线程解析报表

假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。 Way1 join import java.time.LocalTime;public class Main {public static void main(String[] args) thro

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1