介绍

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了。

效果

这里写图片描述

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

results matching ""

    No results matching ""