每日一记:Button一些属性设置

1. Button属性设置
系统提供的Button都是一成不变的样子

系统提供的Button.png

方方正正,浅灰色的背景,好low。

  • Seletor可以为Button提供了在各种状态下显示不同图片或者颜色的选项。想为Button增加点击效果的好选择。

    1. 在res/drawable下创建一个Drawable Resource File,这是一个XML文件
    2. 根元素
      <seletor xmlns:android="http://schemas.android.com/apk/res/android">
      </seletor>
    3. 子元素
      <item></item>
    • 子元素的属性
      • android:drawable 这个属性用来控制该item显示的drawable资源
      • android:state_xxxxx 这个属性用来控制item将在什么情况下显示drawable资源
        其中的xxxxx是泛指许多情况,常用的情况有
        • android:state_focused
        • android:state_selected
        • android:state_pressed
      • 不指定android:state_xxxxx属性的item则是默认项,找不到指定情况下使用的item就是用默认项,注意了,默认项item要写在其他所有item的后面,这个已经坑过我很多次了
    1. 例子 btn_back.xml
      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true" android:drawable="@drawable/back_press" />
      <item android:drawable="@drawable/back" />
      </selector>

    Button中将该btn_back.xml设置为background属性即可

    默认展示

    点击时也就是状态为state_pressed

    可以明显地看到Button的点击效果,虽说这个效果也是可以直接通过java代码实现,可写在XML文件当然方便多了。

  • shape可以为Button提供了修改形状的选项

    1. 在res/drawable下创建一个Drawable Resource File,这是一个XML文件
    2. 根元素
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
      </shape>
    3. 子元素
    • <solid/>-填充颜色
      • ```android:color`` 设置填充的颜色
    • <corners/>-圆角 下列属性来设置圆角的参数
      • android:radius四个角的弧度半径
      • android:bottomLeftRadius左下角的弧度半径
      • android:bottomRightRadius右下角的弧度半径
      • android:topLeftRadius左上角的弧度半径
      • android:topRightRadius右上角的弧度半径
    • <gradient/>-渐变 不常用不详述属性
    • <padding/>-内容离边界距离
      • android:top距离上边界的距离
      • android:bottom距离下边界的距离
      • android:left距离左边界的距离
      • android:right距离右边界的距离
    • <size/>-大小 可在这里设置控件宽度
    • <stroke/>-描边
      • android:color 设置描边的颜色
      • android:width 设置描边的宽度
      • android:dashGap 设置小段实线之间的距离
      • android:dashWidth 设置一小段实线的宽度

    4.例子 btn_shape.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#00ee00"/>
    </shape>

    填充的效果

    <?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient android:startColor="#ff8c00"
    android:endColor="#FFFFFF"
    android:angle="270" />

    <stroke android:width="2dp"
    android:color="#dcdcdc" />

    <corners android:bottomLeftRadius="36dp"
    android:topRightRadius="8dp"/>

    <padding android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />
    </shape>
    综合效果

    Button中将该btn_shape.xml设置为background属性即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,901评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,852评论 0 17
  • 概述 今天我们来探究一下android的样式。其实,几乎所有的控件都可以使用 background属性去引用自定义...
    CokeNello阅读 10,367评论 1 19
  • 我一世的金戈铁马,挥剑杀伐 最终败在了你温文尔雅,萝纱伞下 爱是与你四海为家,携手天涯 得到你执笔作画,瑟瑟风雅 ...
    陌零落阅读 1,319评论 0 2
  • 而这多余的一生 你们相遇,你垮掉 而这不幸的一生 你们生离,你垮掉 好好存在 你不许愿,不许无聊的诺言 好好存在 ...
    樟叶阅读 1,153评论 0 0

友情链接更多精彩内容