Android Selector全解

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"  <!-- true非触摸模式下获得焦点时显示图片-->
android:state_window_focused="true"  <!--true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。 -->
android:state_enabled="true" <!--true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。 -->
android:state_checkable="true" <!--true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片 -->
android:state_checked="true" <!-- true,当CheckBox选中时显示该图片;false,当CheckBox为未选中时显示该图片-->
android:state_selected="true" <!--true 选择时显示的图片;false 为选择时的图片(例如用在Tab) -->
android:state_pressed="true" <!-- true,当被点击时显示该图片;false没被按下时显示图片 ==click-->                                                                    <!--多用于ListView或者切换 -->
android:state_activated="true" <!--true 被激活时显示图片  ;false未激活时图片-->
android:state_active="true" <!-- -->
android:state_single="true" <!-- true 只有一个元素显示图片-->
android:state_first="true" <!-- -->
android:state_middle="true" <!-- -->
android:state_last="true" <!-- -->
android:state_accelerated="true" <!--true 硬件加速为true的图片 -->
android:state_hovered="true" <!-- true 鼠标(指针)移动到该位置时图片-->
android:state_drag_hovered="true"<!-- true 能够drop 或者 drag 鼠标指针移动到该位置图片-->
 android:state_drag_can_accept="true" <!--true 能够dorp 或者 drag 时图片-->
android:state_accessibility_focused="true" <!-- -->
android:drawable="@drawable/icon"<!-- (一般)必备  上文的图片-->
 /> 
  </selector>

场景一: 

<?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>

场景二: 

<?xml version="1.0" encoding="utf-8"?>
 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:state_selected="true" android:color="#FF0000" />
 
    <item android:state_focused="true" android:color="#00FF00" />
 
    <item android:state_pressed="true" android:color="#0000FF" />
 
    <item android:color="#000000" />
 
</selector>

场景三: 

<?xml version="1.0" encoding="utf-8"?>
 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:state_pressed="true">
 
        <!-- 定义当button 处于pressed 状态时的形态。-->
 
        <shape>
 
            <gradient android:startColor="#8600ff" />
 
            <stroke android:width="2dp" android:color="#000000" />
 
            <corners android:radius="5dp" />
 
            <padding android:left="10dp" android:top="10dp"
 
                android:bottom="10dp" android:right="10dp" />
 
        </shape>
 
    </item>
 
    <item android:state_focused="true">
 
        <!-- 定义当button获得focus时的形态-->
 
        <shape>
 
            <gradient android:startColor="#eac100" />
 
            <stroke android:width="2dp" android:color="#333333" color="#ffffff" />
 
            <corners android:radius="8dp" />
 
            <padding android:left="10dp" android:top="10dp"
 
                android:bottom="10dp" android:right="10dp" />
 
        </shape>
 
    </item>
    <item android:drawable="@drawable/you_picture_name">默认背景</item>
</selector>

这里我们给出重点提醒selector  一般用于background

不要忘记

android:background="@drawable/you_drawable_xml_name"

猜你喜欢

转载自blog.csdn.net/qq_36473318/article/details/88043540