美文网首页首页投稿(暂停使用,暂停投稿)
Android Button 你真的会用吗(含福利)???

Android Button 你真的会用吗(含福利)???

作者: 魔术师6967 | 来源:发表于2016-04-19 17:48 被阅读3283次

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

<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,但它有自己的属性:

<shape

xmlns:android="http://schemas.android.com/apk/res/android"

android:shape=["rectangle"|"oval"|"line"|"ring"]>

//一般用rectangle,将高和宽设置相等可以得到圆

<corners

android:radius="integer"        //统一圆角半径

android:topLeftRadius="integer"

android:topRightRadius="integer"

android:bottomLeftRadius="integer"

android:bottomRightRadius="integer"/>

<gradient

//背景颜色梯度变化,如为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"]/>

<padding

android:left="integer"

android:top="integer"

android:right="integer"

android:bottom="integer"/>

<size

android:width="integer"

android:height="integer"/>

<solid

android:color="color"/>

<stroke

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

相关文章

  • Android Button 你真的会用吗(含福利)???

    Button在Android开发中可以说是无处不在,一般在xml中声明,作为与用户交互的常用组件,我们不仅仅让其显...

  • AsyncTask

    Android开发者:你真的会用AsyncTask吗? 导读.1 在Android应用开发中,我们需要时刻注意保证...

  • Android adb你真的会用吗?

    引言 本文基于Android官方文档, 以及个人工作的使用经验, 总结下adb的常用用法, 备忘. 1, adb简...

  • 什么是ISSUE

    你真的会用issue吗?

  • Android周报第四期

    1)一步一步教你实现Periscope点赞效果 2)Android开发者:你真的会用AsyncTask吗? 重新审...

  • 你真的会用Android中Strings资源吗

    Android为了帮助开发者把应用更方便发布给全球不同语言的人们使用,建议开发者在进行开发时不要把UI呈现相关的文...

  • Gson使用指南 2017-08-15

    Gson使用指南系列其它文章你真的会用Gson吗?Gson使用指南(一)你真的会用Gson吗?Gson使用指南(二...

  • Andriod中Gson使用

    Gson使用 参考你真的会用Gson吗?Gson使用指南 在Android中使用Gson,先在Compile中加入...

  • Block使用注意

    在项目中,我们经常会用到block,但是说起block你真的会用吗?你真的全都了解吗?如果心里犯嘀咕的话,那么就往...

  • 你真的会用HttpMessageConverter吗?

    前言 最近看了Spring MVC源码,感觉特别有趣,像发现了新大陆一般,不能自拔。图片来自互联网.png图片来自...

网友评论

    本文标题:Android Button 你真的会用吗(含福利)???

    本文链接:https://www.haomeiwen.com/subject/axawlttx.html