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>

 
圆形的点击颜色变化.gif

.
.
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的焦点变化.gif

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"/>
 
下划线.gif

了解更多的Drawable分类 Drawable图像资源抽象类
本篇完


相关参考

Android之drawable state各个属性详解



作者:阿敏其人
链接:https://www.jianshu.com/p/7257ce82c762
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/tc310/p/8978056.html