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成 ...
随机推荐
- Kubernetes--kubectl
一.Kubectl命令行说明 类型 命令 描述 基础命令 create 通过文件名或标准输入创建资源 expose 将一个资源公开为一个新的kubernetes服务 run 创建并运行一个特定的镜 ...
- P1621 集合
P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...
- python中的文件操作
文件操作时,有'r','w','a'不同的操作类型,其中'r'只能读文件,seek(),tell()函数定位读的起始地方.'w'会清空文件内容然后写文件,seek(),tell()函数定位写的起始地方 ...
- 科学计算三维可视化---TraitsUI(控件)
一:文本编辑器 from traits.api import HasTraits,Int,Str,Password from traitsui.api import View,Item,Group,M ...
- (转)tomcat+nginx+redis实现均衡负载、session共享(一)
在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一些状况: 1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一) ...
- LeetCode-330.Patching Array
/** * nums的所有元素,假设最大能连续形成[1,sum] 当增加一个element的时候 * 会变成 [1,sum] [element+1,sum+element]两个区间,这两个区间有以下可 ...
- [转载]mysql下载安装
转自https://www.cnblogs.com/tyhj-zxp/p/6693046.html 下载 打开:https://www.mysql.com/downloads/ 1.点击该项:
- python核心编程笔记——Chapter7
Chapter7.映像和集合类型 最近临到期末,真的被各种复习,各种大作业缠住,想想已经荒废了python的学习1个月了.现在失去了昔日对python的触觉和要写简洁优雅代码的感觉,所以临到期末毅然继 ...
- soj1046. Plane Spotting
1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond o ...
- 最长递增子序列(LIS)(转)
最长递增子序列(LIS) 本博文转自作者:Yx.Ac 文章来源:勇幸|Thinking (http://www.ahathinking.com) --- 最长递增子序列又叫做最长上升子序列 ...