Button在Android开发中可以说是无处不在,一般在xml中声明,作为与用户交互的常用组件,我们不仅仅让其显示和监听就觉得ok了,button还有很多其他有用和有趣的功能待你使用。

Button介绍


view继承关系

从View继承图来看,Button的父类是TextView,说明Button拥有TextView的属性,包括

android:drawableTop   上图下文

android:autoLink          超链接

android:inputType        限制输入类型

等等... 

 当然这些都是最基本,接下来介绍更好玩的Button属性设置

变化的Button

——selector

  当你希望Button按下会出现变化,此时需要在android:background设置 a stateListDrawable,顾名思义,这是一个有一系列状态的图片资源,你需要在drawable目录下定义这个xml文件,如下:

 文件命名为 button_state.xml

android:constantSize=["true"|"false"]  //固定尺寸,默认false

android:dither=["true"|"false"]        // 缩略图匹配当前像素,默认true

android:variablePadding=["true"|"false"]>  //匹配当前padding,默认false

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设置为如下:

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"/>

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指南:

Drawable Resources

  最后提示一下刚踏进android开发的小白同志,大神可以忽略:

button的监听可以在Java代码设置button.setOnClickListener;也可以在xml中声明android:onClick="selfDestruct",然后在代码中设置

public void selfDestruct(Viewview){\\\\}

  注意selfDestruct要一致

FloatingActonButton

作为Material Design 的特色组件,fab具有很多动画效果功能,如下图所示


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等。

参考:01 Material Design之FloatingActionButton的使用

         02 Android Developer Button

更多相关文章

  1. Android(安卓)数据操作(一) 自定义AttributeSet属性
  2. Android与H5互调使用cordova的WebView,插件调用,插件编写
  3. Android(安卓)APP 漏洞整理
  4. 安卓系统的启动流程
  5. android 在线预览pdf文件(目前最全)
  6. Android(安卓)FFmpeg移植总攻略——获取视频帧数(亲测可用)
  7. android NDK学习篇4之two-libs——使用(单个动态库)来生成动态库
  8. Android7.0适配教程与心得
  9. EditText控件设置只读,动态控制EditText状态

随机推荐

  1. Android(安卓)开发笔记 动画效果 --Anima
  2. Android弹出软键盘布局是否上移问题
  3. android 属性android:visibility
  4. Android的线程Handler实现
  5. Intellij下的android实践
  6. android体系介绍
  7. Android(安卓)Launcher研究(一)---------
  8. 用Android代码实现自动打开USB调试
  9. Android请求网络有三种方式:HttpURLConnec
  10. Android: 自定义Tab样式