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>
 
圆形的点击颜色变化.gif

.
.
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的焦点变化.gif

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"/>
 
下划线.gif

了解更多的Drawable分类 Drawable图像资源抽象类
本篇完


相关参考

Android之drawable state各个属性详解

作者:阿敏其人
链接:https://www.jianshu.com/p/7257ce82c762
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Drawable子类之——StateListDrawable (选择器)的更多相关文章

  1. 使用(Drawable)资源——StateListDrawable资源

    StateListDrawable用于组织多个Drawable对象.当使用StateListDrawable作为目标组件的背景.前景图片时,StateListDrawable对象所显示的Drawabl ...

  2. android的Drawable详解

    Drawable简介 Drawable有很多种,用来表示一种图像的概念,但他们又不完全是图像,他们是用过颜色构建出来的各种图像的表现形式.Drawable一般都是通过xml来定义的 ,当然我们也可以通 ...

  3. Android中的Drawable资源

    在Android应用中,常常会用到Drawable资源,比如图片资源等,在Android开发中我们是用Drawable类来Drawable类型资源的. Drawable资源一般存储在应用程序目录的\r ...

  4. Android Drawable绘图学习笔记(转)

    如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...

  5. 六、Drawable

    Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念. 1.Drawable简介 Drawable是一个抽象类,是所有Drawable对象的基类,每个具体的Drawable都是它的子类 ...

  6. android中常见的Drawable资源有哪些?

    Drawable资源是安卓应用中最常见的一种资源,比如图片等,因此,对于初学者而言,必须掌握drawable资源相关应用. 今天在网上刚好看到了一篇介绍android Drawable资源的文章,分享 ...

  7. Android APK开发 Drawable文件夹下的自定义Drawable文件

    版本:2018/2/11 Drawable的分类 自定义Drawable SVG矢量图 个人总结的知识点外,部分知识点选自<Android开发艺术探索>-第六章 Drawable 1.Dr ...

  8. 进阶之路 | 奇妙的Drawable之旅

    前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习清单: Drawable简介 Drawable分类 自定义Drawable 一.为什么要学习Drawabl ...

  9. Android之Drawable

    Android 中图片和常见的颜色都可以是一个Drawable. Drawable可以方便我们做出一些特殊的UI效果,这一点在UI相关的开发工作中尤其重要.其主要优点有: 使用简单,比自定义View成 ...

随机推荐

  1. P2684 搞清洁

    P2684 搞清洁 给定一段区间及若干个线段, 求使区间被完全覆盖所需的最少线段数 错误日志: 菜 Solution 补一下贪心吧 这题最小线段覆盖 首先按照左端点排序 现在对于所有左区间到达目前已覆 ...

  2. 数据分析与展示---Matplotlib基本绘图函数

    一:基本绘图函数(这里介绍16个,还有许多其他的) 二:pyplot饼图plt.pie的绘制 三:pyplot直方图plt.hist的绘制 (一)修改第二个参数bins:代表直方图的个数,均分为多段, ...

  3. C++程序运行时间测定

    From:http://www.cnblogs.com/killerlegend/p/3877703.html Author:KillerLegend Date:2014.7.30 此处程序的测试时间 ...

  4. COGS 5. P服务点设置

    5. P服务点设置 http://www.cogs.pro/cogs/problem/problem.php?pid=5 ★★   输入文件:djsc.in   输出文件:djsc.out   简单对 ...

  5. warshall-floyd算法:POJ No 2139 Six Degress of Cowvin Bacon(任意两点最短路径))

    题目: http://poj.org/problem?id=2139 题解:N只牛,在一组的两只牛,分别两只之间为 “1度”,自己到自己为0度,M组牛.求,N只牛之中,两只牛之间 平均最短度数*100 ...

  6. jquery如何获取input(file)控件上传的图片名称,即"11111.jpg"

    html代码:<input name=file" type="file" id="file"/> Jquery代码:var file;$( ...

  7. 微信小程序开发教程(六)配置——app.json、page.json详解

    全局配置:app.json 微信小程序的全局配置保存在app.json文件中.开发者通过使用app.json来配置页面文件(pages)的路径.窗口(window)表现.设定网络超时时间值(netwo ...

  8. POJ 3783 Balls --扔鸡蛋问题 经典DP

    题目链接 这个问题是谷歌面试题的加强版,面试题问的是100层楼2个鸡蛋最坏扔多少次:传送门. 下面我们来研究下这个题,B个鸡蛋M层楼扔多少次. 题意:给定B (B <= 50) 个一样的球,从 ...

  9. 【leetcode 简单】 第七十题 有效的字母异位词

    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" ...

  10. js 各类判断用户输入字符的格式函数

    1.JS 判断IP格式是否正确: function checkIP(ip) { var regular = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//正则表达式 if (reg ...