android selector的item属性

<selector>中item的属性问题:

部分属性如下:

android:state_pressed  :  true/false   //表示按下状态(例如按钮按下)
android:state_focused  :  true/false   //表示聚焦状态(例如使用滚动球/D-pad聚焦Button)
android:state_selected :  true/false   //表示选中状态(例如Tab 打开);
android:state_checkable:  true/false   //表示可勾选状态(只对能切换可勾选—非可勾选的构件有用)
android:state_checked  :  true/false   //表示勾选状态
android:state_enabled  :  true/false   //表示可用状态
android:window_focused :  true/false   //表示应用程序窗口有无焦点

注意:selector中总是使用第一个匹配的item

例:布局文件layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">     
    <EditText
        android:background="@drawable/bg_edittext"  //使用选择器
        android:textColorHint="#AAAAAA"
        android:hint="username"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
</LinearLayout>

选择器文件drawable/bg_edittext.xml

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 默认时的背景图片-->
    <item android:drawable="@drawable/pic1" />
    <!-- 没有焦点时的背景图片-->
    <item android:state_window_focused="false" android:drawable="@drawable/pic1" />
    <!-- 非触摸模式下获得焦点并单击时的背景图片-->
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/pic2" />
    <!-- 触摸模式下单击时的背景图片-->
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/pic3" />
    <!--选中时的图片背景-->
    <item android:state_selected="true" android:drawable="@drawable/pic4" />
    <!--获得焦点时的图片背景-->
    <item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>
原文地址:https://www.cnblogs.com/mengfff/p/6764789.html