Button在Android开发中可以说是无处不在,一般在xml中声明,作为与用户交互的常用组件,我们不仅仅让其显示和监听就觉得ok了,button还有很多其他有用和有趣的功能待你使用。
Button介绍
从View继承图来看,Button的父类是TextView,说明Button拥有TextView的属性,包括
android:drawableTop 上图下文
android:autoLink 超链接
android:inputType 限制输入类型
等等...
当然这些都是最基本,接下来介绍更好玩的Button属性设置
变化的Button
——selector
当你希望Button按下会出现变化,此时需要在android:background设置 a stateListDrawable,顾名思义,这是一个有一系列状态的图片资源,你需要在drawable目录下定义这个xml文件,如下:
文件命名为 button_state.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true"|"false"] //固定尺寸,默认false
android:dither=["true"|"false"] // 缩略图匹配当前像素,默认true
android:variablePadding=["true"|"false"]> //匹配当前padding,默认false
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true"|"false"]
android:state_focused=["true"|"false"]
android:state_hovered=["true"|"false"]
android:state_selected=["true"|"false"]
android:state_checkable=["true"|"false"]
android:state_checked=["true"|"false"]
android:state_enabled=["true"|"false"]
android:state_activated=["true"|"false"]
android:state_window_focused=["true"|"false"]/>
//当以上某行为true,用户交互切换到那个状态时,执行action;
在layout.xml文件中,需要将button的background设置为如下:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button_state"/>
---shape
但android开发中更多地是使用不同形状的button(例如圆形图,圆角矩形图),此时就不是使用selector,而是使用shape关键字,代码设置类似上面的selector,但它有自己的属性:
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle"|"oval"|"line"|"ring"]>
//一般用rectangle,将高和宽设置相等可以得到圆
android:radius="integer" //统一圆角半径
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer"/>
//背景颜色梯度变化,如为solid则没有渐变
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear"|"radial"|"sweep"]
android:useLevel=["true"|"false"]/>
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer"/>
<size
android:width="integer"
android:height="integer"/>
android:color="color"/>
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer"/>
在layout文件中设置请参考selector
传送门:轻轻松松、半分钟完成复杂的Button
button利器:Android Button Maker
当然,除了selector、shape,还有scale,transition 等模式,具体可参考android 官方 API指南:
最后提示一下刚踏进android开发的小白同志,大神可以忽略:
button的监听可以在Java代码设置button.setOnClickListener;也可以在xml中声明android:onClick="selfDestruct",然后在代码中设置
public void selfDestruct(Viewview){\\\\}
注意selfDestruct要一致
FloatingActonButton
作为Material Design 的特色组件,fab具有很多动画效果功能,如下图所示
fab继承与ImageView,拥有ImageView的一切属性,但它有自己的一个特性,注意命名空间是 xmlns:app="http://schemas.android.com/apk/res-auto",下面介绍它的属性:
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等。