Drawable selector detailed explanation

No

xml tag

Class class

meaning

1

shape

ShapeDrawable

specific shape, pattern of a model

2

selector

StateListDrawable

Choose different patterns for different states

3

layer-list

LayerDrawable

cascading pattern

4

level-list

LevelListDrawable

Different levels of patterns

5

transition

TransitionDrawable

gradient pattern

6

ripple

RippleDrawable

ripple pattern

7

inset

InsetDrawable

Embedded graphics

8

scale

ScaleDrawable

scale pattern

9

clip

ClipDrawable

cut pattern

10

rotate

RotateDrawable

Rotate pattern

11

animation-list

AnimationDrawable

Animation effects

12

bitmap

BitmapDrawable

Picture pattern

13

nine-patch

NinePatchDrawable

.9 Figure

In Android, regardless of the size of the project, you can see the selector status selector. It is generally used in various operating states, mainly reflected in the switching of fonts and backgrounds. We can dynamically use code to set it! You can also use the selector state selector to implement it quickly, because it is simple, convenient and highly reusable! 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="3000"
    android:enterFadeDuration="3000"
    >

    <!--
   //设置是否按压状态,一般在true时设置该属性,表示已按压状态,默认为false

    android:state_pressed

    //设置是否选中状态,true表示已选中,false表示未选中

    android:state_selected

    //设置是否勾选状态,主要用于CheckBox和RadioButton,true表示已被勾选,false表示未被勾选

    android:state_checked

    //设置勾选是否可用状态,类似state_enabled,只是state_enabled会影响触摸或点击事件,state_checkable影响勾选事件

    android:state_checkable

    //设置是否获得焦点状态,true表示获得焦点,默认为false,表示未获得焦点

    android:state_focused

    //设置触摸或点击事件是否可用状态,一般只在false时设置该属性,表示不可用状态

    android:state_enabled

    较少用:
    //设置当前窗口是否获得焦点状态,true表示获得焦点,false 表示未获得焦点,例如拉下通知栏或弹出对话框时, 当前界面就会失去焦点;另外,ListView的ListItem获得焦点时也会触发true状态,可以理解为当前窗口就是ListItem本身

    android:state_window_focused

    //设置是否被激活状态,true表示被激活,false表示未激活,API Level 11及以上才支持,可通过代码调用控件的

    android:state_activated

    //方法设置是否激活该控件
    setActivated(boolean)

    //设置是否鼠标在上面滑动的状态**,true表示鼠标在上面滑动,默认为false,API Level 14及以上才支持
    //补充:selector标签下有两个比较有用的属性要说一下,添加了下面两个属性之后,则会在状态改变时出现淡入淡出效果,
    //但必须在API Level 11及以上才支持

    android:state_hovered

    //状态改变时,旧状态消失时的淡出时间,以毫秒为单位

    android:exitFadeDuration

    //状态改变时,新状态展示时的淡入时间,以毫秒为单位

    android:enterFadeDuration

-->

    <!--设置点击改变背景,再点击返回背景-->
    <item android:state_activated="true"   android:drawable="@drawable/shape_rectangle"/>
    <item android:drawable="@drawable/shape_ring" android:state_activated="false"/>

    <!--点击改变背景-->
    <item android:state_pressed="true" android:drawable="@drawable/shape_rectangle" />
    <item android:drawable="@drawable/shape_ring" />

</selector>

 

Guess you like

Origin blog.csdn.net/paroleg/article/details/103539537