<Android 基础(十四)> selector
2016-07-05 08:03
411 查看
介绍
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自定义的圆弧型控件 RoundView
- 【Android】33、常见控件的使用方法——ProgressDialog
- Android Matrix利用invert实现逆向坐标映射
- Android之Json解析
- Android 中File.delete()无法正常删除文件夹
- 讯飞语音开发之语音唤醒
- Android SDK Manager 中如果没有相应的镜像ARM XX Image
- Android Studio官方文档之工程项目概述篇
- Android SDK无法更新解决方式(非常有效)
- view的setTag() 和 getTag()应用
- 第二课:Android Studo 各文件作用-20160705
- Android Studio编译输出apk文件修改文件名
- Android开发者已经渡过了初级吗
- Android intent传递list或对象
- AndroidManifest问题
- 第一课:如何创建Android Studo 工程-20160705
- Android拍照上传
- android studio 源代码无法关联
- Android签名机制介绍:生成keystore.签名.查看签名信息等方法
- AndroidAnnotations注解说明