Drawable子类之——StateListDrawable (选择器)
Drawable子类之——StateListDrawable (选择器)
https://www.jianshu.com/p/7257ce82c762
本文出自 “阿敏其人” 简书博客,转载或引用请注明出处。
StateListDrawable对应的XML根元素是<selector>,它可以根据View的状态的不同匹配展示不同的Drawable。比如点击时背景是红色,不点击时时白色,所以StateListDrawable鲳鱼点击事件的背景。
我们常常给按钮的按下的时候设定一个特殊的背景,大概如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/little_gray"></item>
<item android:state_focused="true" android:drawable="@color/little_gray"></item>
<item android:drawable="@color/big_bg_color"></item>
</selector>
一、语法
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "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"] />
</selector>
二、子节点
iStateListDrawable对应的XML根元素是<selector>,它可以根据View的状态的不同匹配展示不同的Drawable。比如点击时背景是红色,不点击时时白色,所以StateListDrawable鲳鱼点击事件的背景。
一、语法
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "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"] />
</selector>
二、子节点
子节点要关心只有 selector里面的属性和item里面的状态。
每一个item表示一个Drawable,item里面放什么怎么主要关系不大,我们关注的只有item的状态。
android:constantSize
StateListDrawable的大小是否随着View的状态的改变而改变。true固定大小,不随之改变,false为随着改变拉伸自身大小。
默认为false。
.
.android:dither
是否开启抖动,开。
.
.android:variablePadding
默认false,建议false
是否随着View的状态的改变而改变padding,如果为true,padding的状态会随着改变,如果为false,那么就采用item内部的Drawable自身设定的padding的值。主要关心item里面的Drawable的状态判定
| 状态 | 含义 |
|---|---|
| android:state_pressed | 按下的状态,(按下但是还没松开) |
| android:state_focused | 当前View获取了焦点 |
| android:state_selected | 用户选择了当前View |
| android:state_checked | 用户选中了View,一般用于CheckBox这种非黑即白的选项 |
| android:state_enabled | 当前View处于可用的状态 |
| android:state_hovered | 光标是否悬停,通常与focused state相同,它是4.0的新特性 |
| android:state_checkable | 组件是否能被check。如:RadioButton是可以被check的。 |
| android:state_activated | 是否被激活 |
| android:state_window_focused | 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了 |
三、特点
1、item可以用多个,item里面放的是Drawable
2、系统查找顺序是顺着item从上到下知道找到就停止往下寻找。
四、Demo演示
这里的demo我们在 Drawable子类之—— Drawable子类之—— ShapeDrawable (图形定义)使用过。
圆形的点击变换颜色
<?xml version="1.0" encoding="utf-8"?>
<!--别看这里我们使用的是ovrl(椭圆) ,但是我们得到可是 圆形 的点击效果-->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="oval">
<solid
android:color="#ff0000" />
<stroke
android:width="4dp"
android:color="#294736" />
</shape>
</item>
<item >
<shape android:shape="oval">
<solid
android:color="#848374" />
<stroke
android:width="4dp"
android:color="#745863" />
</shape>
</item>
</selector>

.
.
Edittext的背景框和焦点变化
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_window_focused="false">
<shape android:shape="rectangle">
<solid
android:color="#FFFFFFFF"/>
<corners
android:radius="3dp"/>
<padding
android:left="10dp"
android:right="10dp"/>
<stroke
android:width="1dp"
android:color="#BDC7D8"/>
</shape>
</item>
<item android:state_focused="true" >
<shape android:shape="rectangle" >
<solid
android:color="#FFFFFFFF"/>
<corners
android:radius="3dp"/>
<padding
android:left="10dp"
android:right="10dp"/>
<stroke
android:width="1dp"
android:color="#728ea3"/>
</shape>
</item>
</selector>

Edittext输入框
selector_edittext_line
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="@color/deep_orange" />
</shape>
</item>
<item >
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="@color/orange" />
</shape>
</item>
</selector>
使用
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="@null"
android:drawableBottom="@drawable/selector_edittext_line"
android:hint="@string/user_name"
android:paddingLeft="10dip"
android:singleLine="true"
android:textColor="#000"
android:textSize="18sp"/>

了解更多的Drawable分类 Drawable图像资源抽象类
本篇完
相关参考
作者:阿敏其人
链接:https://www.jianshu.com/p/7257ce82c762
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Drawable子类之——StateListDrawable (选择器)的更多相关文章
- 使用(Drawable)资源——StateListDrawable资源
StateListDrawable用于组织多个Drawable对象.当使用StateListDrawable作为目标组件的背景.前景图片时,StateListDrawable对象所显示的Drawabl ...
- android的Drawable详解
Drawable简介 Drawable有很多种,用来表示一种图像的概念,但他们又不完全是图像,他们是用过颜色构建出来的各种图像的表现形式.Drawable一般都是通过xml来定义的 ,当然我们也可以通 ...
- Android中的Drawable资源
在Android应用中,常常会用到Drawable资源,比如图片资源等,在Android开发中我们是用Drawable类来Drawable类型资源的. Drawable资源一般存储在应用程序目录的\r ...
- Android Drawable绘图学习笔记(转)
如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...
- 六、Drawable
Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念. 1.Drawable简介 Drawable是一个抽象类,是所有Drawable对象的基类,每个具体的Drawable都是它的子类 ...
- android中常见的Drawable资源有哪些?
Drawable资源是安卓应用中最常见的一种资源,比如图片等,因此,对于初学者而言,必须掌握drawable资源相关应用. 今天在网上刚好看到了一篇介绍android Drawable资源的文章,分享 ...
- Android APK开发 Drawable文件夹下的自定义Drawable文件
版本:2018/2/11 Drawable的分类 自定义Drawable SVG矢量图 个人总结的知识点外,部分知识点选自<Android开发艺术探索>-第六章 Drawable 1.Dr ...
- 进阶之路 | 奇妙的Drawable之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习清单: Drawable简介 Drawable分类 自定义Drawable 一.为什么要学习Drawabl ...
- Android之Drawable
Android 中图片和常见的颜色都可以是一个Drawable. Drawable可以方便我们做出一些特殊的UI效果,这一点在UI相关的开发工作中尤其重要.其主要优点有: 使用简单,比自定义View成 ...
随机推荐
- linux 中 virtualenvwrapper的使用
原文链接:http://www.jianshu.com/p/3abe52adfa2b Virtaulenvwrapper Virtaulenvwrapper是virtualenv的扩展包,用于更方便管 ...
- group by实现原理及其作用
mysql中group by实现方式有三种,松散索引,紧凑索引,临时文件(文件排序). 在网上看了相关的介绍,大部分介绍都比较晦涩难懂,这里说下我的理解. 在学习SQL优化时,我们都知道可以对grou ...
- 科学计算三维可视化---Mlab基础(常用控制函数)
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 洛谷P3953 [NOIP2017]逛公园
K<=50,感觉可以DP 先建反图求出从n到各个点的最短路,然后在正图上DP 设f[当前点][比最短路多走的距离]=方案数 转移显然是 $f[v][res]=\sum f[u][res+tmp] ...
- Element-UI 表格 列过多内容换行问题
本文地址:http://www.cnblogs.com/veinyin/p/8487098.html 一般表格不会有很多列,所以在使用时会很方便,但是如果有25+个列时,就会发现宽度完全不够用,只有 ...
- 在window 8 或windows2012 上用命令行安装framework3.5 方法
找到对应操作系统安装目录的sources文件夹下的sxs文件夹,拷贝到本地电脑,如F:盘 根目录下 CMD(管理员身份)命令: dism.exe /online /enable-feature /fe ...
- cin.get()和cin.getline()之间的区别
cin.getline()和cin.get()都是对输入的面向行的读取,即一次读取整行而不是单个数字或字符,但是二者有一定的区别. cin.get()每次读取一整行并把由Enter键生成的换行符留在输 ...
- Mac 下 gzip 一个文件
gzip -k xxx.json -k 会保留源文件
- aarch64_m1
MAKEDEV-3.24-18.fc26.aarch64.rpm 2017-02-14 08:46 99K fedora Mirroring Project MUMPS-5.0.2-8.fc26.aa ...