<Android 基础(十四)> selector
介绍
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的更多相关文章
- Android基础测试题(四)
看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...
- Bootstrap<基础十四> 按钮下拉菜单
使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉菜单,只需要简单地在在一个 .btn-group 中放置按钮和下拉菜单即可.也可以使用 <span class=&qu ...
- Android入门(十四)内容提供器-实现跨程序共享实例
原文链接:http://www.orlion.ga/661/ 打开SQLite博文中创建的 DatabaseDemo项目,首先将 MyDatabaseHelper中使用 Toast弹出创建数据库成功的 ...
- android学习十四(android的接收短信)
收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...
- android基础(四)service
Service的两种启动方式:startService()与bindService() statService:生命周期:[onCreate()- >onStartCommand()-&g ...
- Android进阶(十四)Android Adapter详解
Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...
- C++学习基础十四——基础类型vector
一.vector的使用 1. #include <vector> 2. 初始化的四种方法 vector<T> v1; vector<T> v2(v1); vecto ...
- android 学习十四 探索安全性和权限
1.部署安全性:应用程序必须使用数字证书才能安装到设备上. 2.执行期间的安全性: 2.1 使用独立进程 2.2 使用固定唯一用户ID 2.3 申明性权限模型 3数字证书 ...
- <Android 基础(四)> RecyclerView
介绍 RecyclerView是ListView的豪华增强版.它主要包含以下几处新的特性,如ViewHolder,ItemDecorator,LayoutManager,SmothScroller以及 ...
- java基础(十四)-----详解匿名内部类——Java高级开发必须懂的
在这篇博客中你可以了解到匿名内部类的使用.匿名内部类要注意的事项.匿名内部类使用的形参为何要为final. 使用匿名内部类内部类 匿名内部类由于没有名字,所以它的创建方式有点儿奇怪.创建格式如下: n ...
随机推荐
- 基本css拼图形
关闭按钮: <em class="close"></em> .close { width: 16px; height: 16px; text-align: ...
- 插头DP学习笔记
插头DP(我也不知道该怎么定义...)是一种类似于洛谷题目([模板]插头DP)的题目 题目特征为: 在棋盘上 某一维的数据范围很小 完全铺满 计数问题 直接看题吧. [模板]插头DP 给出n*m的方格 ...
- Linux下的hosts文件和network文件区别
Linux下的hosts文件和network文件区别 Linux下有两种与计算机名相关的配置文件 1.hosts文件,路径:/etc/hosts,此文间是在网络上使用的, 用于解析计算机名 ...
- centos6.4安装过程中无法出现图形化界面
今天在VMware9.0安装centos6.4的时候,前面的步骤都没问题,到跳过媒体检查这一步后,就发现和之前的不一样了,这次安装不是图形界面,导致后面的安装不方便 一开始以为是centos的版本不一 ...
- C语言中typedef的解释_2
typedef工具是一个高级数据特性.利用typedef可以为某一类型自定义一个新的名称.这样可以提高程序的可读性,可移植性,向用户表明特定用途. typedef没有创建任何新的类型,它只是为某个已存 ...
- Entity Framework 更新带外键的实体为null
using (var ctx = new PortalContext()){ var city = ctx.Cities.Find(42); ctx.Entry(city) ...
- 一般小的maven pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- http 和 https 的区别
参考:http://www.cnblogs.com/wqhwe/p/5407468.html HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从W ...
- hive表多种存储格式的文件大小差异,无重复数据
-- 重点,目标表无重复数据 -- dbName.num_result 无重复记录 -- 插入数据 CREATE TABLE dbName.test_textfile( `key` string, ` ...
- python中bytes类型转换为str类型
使用的原因:基于URL解析报文的时候,要使用str类型,但是提供的确实bytes类型,报错: TypeError: must be str, not bytes 所以就把bytes类型转换为str类型 ...