Android Selector全解

[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8" ?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <item android:state_focused="true"    
  4. android:state_window_focused="true"    
  5. android:state_enabled="true"   
  6. android:state_checkable="true"   
  7. android:state_checked="true"   
  8. android:state_selected="true"   
  9. android:state_pressed="true"                                                                       
  10. android:state_activated="true"   
  11. android:state_active="true"   
  12. android:state_single="true"   
  13. android:state_first="true"   
  14. android:state_middle="true"   
  15. android:state_last="true"   
  16. android:state_accelerated="true"   
  17. android:state_hovered="true"   
  18. android:state_drag_hovered="true"  
  19.  android:state_drag_can_accept="true"   
  20. android:state_accessibility_focused="true"   
  21. android:drawable="@drawable/icon"  
  22.  />   
  23.   selector>  

接下来,给出部分使用场景
[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8" ?>     
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     
  4.   <item android:drawable="@drawable/pic1" />      
  5.     
  6.   <item android:state_window_focused="false"     
  7.         android:drawable="@drawable/pic1" />     
  8.     
  9.   <item android:state_focused="true" android:state_pressed="true"   android:drawable"@drawable/pic2" />   
  10.     
  11. <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />    
  12.     
  13.   <item android:state_selected="true"   android:drawable="@drawable/pic4" />     
  14.     
  15.   <item android:state_focused="true"   android:drawable="@drawable/pic5" />     
  16. selector>  

当然我们也可以这样
[java]  view plain  copy
  1. "1.0" encoding="utf-8"?>  
  2.   
  3. "http://schemas.android.com/apk/res/android">  
  4.   
  5.     "true" android:color="#FF0000" />  
  6.   
  7.     "true" android:color="#00FF00" />  
  8.   
  9.     "true" android:color="#0000FF" />  
  10.   
  11.     "#000000" />  
  12.   
  13.   

或者这样
[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   
  5.     <item android:state_pressed="true">  
  6.   
  7.           
  8.   
  9.         <shape>  
  10.   
  11.             <gradient android:startColor="#8600ff" />  
  12.   
  13.             <stroke android:width="2dp" android:color="#000000" />  
  14.   
  15.             <corners android:radius="5dp" />  
  16.   
  17.             <padding android:left="10dp" android:top="10dp"  
  18.   
  19.                 android:bottom="10dp" android:right="10dp" />  
  20.   
  21.         shape>  
  22.   
  23.     item>  
  24.   
  25.     <item android:state_focused="true">  
  26.   
  27.           
  28.   
  29.         <shape>  
  30.   
  31.             <gradient android:startColor="#eac100" />  
  32.   
  33.             <stroke android:width="2dp" android:color="#333333" color="#ffffff" />  
  34.   
  35.             <corners android:radius="8dp" />  
  36.   
  37.             <padding android:left="10dp" android:top="10dp"  
  38.   
  39.                 android:bottom="10dp" android:right="10dp" />  
  40.   
  41.         shape>  
  42.   
  43.     item>  
  44.     <item android:drawable="@drawable/you_picture_name">默认背景item>  
  45. selector>  

这里我们给出重点提醒selector  一般用于background
不要忘记
[html]  view plain  copy
  1. android:background="@drawable/you_drawable_xml_name"  

你可能感兴趣的:(控件属性)