介绍

A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state.

You can describe the state list in an XML file. Each graphic is represented by an “item” element inside a single “selector” element. Each “item” uses various attributes to describe the state in which it should be used as the graphic for the drawable.

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the “best match,” but simply the first item that meets the minimum criteria of the state.

翻译:

StateListDrawable 是一种通过XML文件来定义的drawable,使用几个不同的图片来呈现同一个图形,通过object的状态来实现切换。例如,一个Button有几个不同的状态(按压,获取焦点等等),这种情况下,通过使用 state list drawable,你就可以实现在不同的状态下使用不同的背景图片。

你可以在一个XML文件中描述state list。通过在根节点selector下定义一个item元素来添加每个图形。每一各item中使用不同的状态属性来定义不用的drawable。

当每一次状态改变的时候,state list都会从上到下被遍历一遍,第一个与当前state相匹配的item将会被使用—- 这个选择并不是作出“最匹配”结果,而是简单的找到第一个匹配的状态。

selector一般都是用来作为有状态改变的View的背景,以此来达到当用户对View进行操作,导致View状态改变时,作出改变,让用户感知View的状态变化。

官方说明

文件位置:res/drawable/filename.xml

编译资源类型:StateListDrawable

资源引用:

In Java: R.drawable.filename

In XML: @[package:]drawable/filename

语法:

<?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>

更多详细说明,请查阅xsoftlab

实际使用

下面做一个简单的实例,对Button的背景根据状态做一下处理

XML文件

selector_ts.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pink" android:state_pressed="true" />
<item android:drawable="@color/yellow" android:state_selected="true" />
<item android:drawable="@drawable/shaperect" android:state_enabled="false" />
<item android:drawable="@color/stone" android:state_enabled="true" />
</selector>

主布局文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="10dp"
tools:context="mraz.com.tabdemo.MainActivity"> <Button
android:id="@+id/bt_content"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/selector_ts" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="horizontal"> <Button
android:id="@+id/bt_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Select"
android:textAllCaps="false" /> <Button
android:id="@+id/bt_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Disable"
android:textAllCaps="false" /> <Button
android:id="@+id/bt_pressed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Press"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>

代码部分 比较简单,这里就不占用过多的篇幅了,看下简单的效果,大家应该就知道如何编写小小的Activity了。

实际效果

更多的内容等待着挖掘机的到来~

<Android 基础(十四)> selector的更多相关文章

  1. Android基础测试题(四)

    看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...

  2. Bootstrap<基础十四> 按钮下拉菜单

    使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉菜单,只需要简单地在在一个 .btn-group 中放置按钮和下拉菜单即可.也可以使用 <span class=&qu ...

  3. Android入门(十四)内容提供器-实现跨程序共享实例

    原文链接:http://www.orlion.ga/661/ 打开SQLite博文中创建的 DatabaseDemo项目,首先将 MyDatabaseHelper中使用 Toast弹出创建数据库成功的 ...

  4. android学习十四(android的接收短信)

    收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...

  5. android基础(四)service

    Service的两种启动方式:startService()与bindService()   statService:生命周期:[onCreate()-  >onStartCommand()-&g ...

  6. Android进阶(十四)Android Adapter详解

    Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...

  7. C++学习基础十四——基础类型vector

    一.vector的使用 1. #include <vector> 2. 初始化的四种方法 vector<T> v1; vector<T> v2(v1); vecto ...

  8. android 学习十四 探索安全性和权限

    1.部署安全性:应用程序必须使用数字证书才能安装到设备上. 2.执行期间的安全性:    2.1 使用独立进程    2.2 使用固定唯一用户ID    2.3  申明性权限模型   3数字证书   ...

  9. <Android 基础(四)> RecyclerView

    介绍 RecyclerView是ListView的豪华增强版.它主要包含以下几处新的特性,如ViewHolder,ItemDecorator,LayoutManager,SmothScroller以及 ...

  10. java基础(十四)-----详解匿名内部类——Java高级开发必须懂的

    在这篇博客中你可以了解到匿名内部类的使用.匿名内部类要注意的事项.匿名内部类使用的形参为何要为final. 使用匿名内部类内部类 匿名内部类由于没有名字,所以它的创建方式有点儿奇怪.创建格式如下: n ...

随机推荐

  1. 【NOIP 2011】Mayan游戏(搜索+模拟)

    描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.**游戏通关是指在规定的步数 ...

  2. 最长回文串:LeetCode:Longest Palindromic Substring

    class Solution { public: string longestPalindrome(string s) { int length=s.length(); ; ; ][]={false} ...

  3. pytorch搭建网络,保存参数,恢复参数

    这是看过莫凡python的学习笔记. 搭建网络,两种方式 (1)建立Sequential对象 import torch net = torch.nn.Sequential( torch.nn.Line ...

  4. css3 matrix 矩阵

    2D矩阵变换 matrix(1,0,0,1,0,0) 对应 matrix (a,b,c,d,e,f) 其中,x, y表示转换元素的所有坐标(变量)了, 3*3矩阵每一行的第1个值与后面1*3的第1个值 ...

  5. BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!

    真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...

  6. linux 底层 基础命令 路径信息

    z基础命令: 打印 :echo "hello world“ 切换目录   cd  / 显示当前路径     pwd 显示 目录下所有文件     ls 显示所有文件包括隐藏文件    ls ...

  7. asp.net mvc AjaxHelper 获取 JSON 的方法

    默认的 AjaxHelper 没有提供获取 JSON 的方法,只提供获取 html 然后更新指定元素的方法,不过,经测试发现还是有办法的,由于 AjaxOptions 对象的 OnSuccess 属性 ...

  8. 增加tomcat多实例

    第一步:解压 第二步:修改端口 /data/service/tomcat1/conf <Server port="8006" shutdown="SHUTDOWN& ...

  9. java实现压缩文件下载

    转载:https://www.cnblogs.com/zeng1994/p/7862288.html

  10. IDEA修改git账号及密码的方法

    IDEA修改git账号及密码的方法: 1.file->settings->passwords 2.重启IDEA 3.执行一次提交或更新 当执行提交或更新之后,idea会自动提示输入账号.密 ...