Android selector 及 Shape

1:Selector html

drawable的item中能夠有如下屬性: 
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"] 
android:state_checked=["true" | "false"] 
android:state_enabled=["true" | "false"] 
android:state_window_focused=["true" | "false"] 
android

[html] view plain copy測試

  1. <?xml version="1.0" encoding="utf-8"?>  spa

  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  .net

  3.     <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true"  3d

  4.           android:drawable="@drawable/enabled_on_pressed" />  orm

  5.     <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true"  xml

  6.           android:drawable="@drawable/enabled_off_pressed" />  htm

  7.     <item android:state_enabled="true" android:state_checked="true"  blog

  8.           android:drawable="@drawable/enabled_on" />  

  9.     <item android:state_enabled="true" android:state_checked="false"  

  10.           android:drawable="@drawable/enabled_off" />  

  11.     <item android:state_enabled="false" android:state_checked="true"  

  12.           android:drawable="@drawable/disabled_on" />  

  13.     <item android:state_enabled="false" android:state_checked="false"  

  14.           android:drawable="@drawable/disabled_off" />                

  15. </selector>  

Item順序是有講究的,條件限定越細緻,則應該放到前面。好比這兒若是把1,2行和3,4行的item交換,那麼pressed的就永遠沒法觸發了,由於有item已經知足條件返回了。能夠理解爲代碼中的if語句。 

 

2:Shape

[html] view plain copy

  1. <shape>  

  2.             <!-- 實心 -->  

  3.             <solid android:color="#ff9d77"/>  

  4.             <!-- 漸變 -->  

  5.             <gradient  

  6.                 android:startColor="#ff8c00"  

  7.                 android:endColor="#FFFFFF"  

  8.                 android:angle="270" />  

  9.             <!-- 描邊 -->  

  10.             <stroke  

  11.                 android:width="2dp"  

  12.                 android:color="#dcdcdc" />  

  13.             <!-- 圓角 -->  

  14.             <corners  

  15.                 android:radius="2dp" />  

  16.             <padding  

  17.                 android:left="10dp"  

  18.                 android:top="10dp"  

  19.                 android:right="10dp"  

  20.                 android:bottom="10dp" />  

  21.         </shape>  

solid:實心,就是填充的意思
android:color指定填充的顏色
gradient:漸變
android:startColor和android:endColor分別爲起始和結束顏色,ndroid:angle是漸變角度,必須爲45的整數倍
另外漸變默認的模式爲android:type="linear",即線性漸變,能夠指定漸變爲徑向漸變,android:type="radial",徑向漸變須要指定半徑android:gradientRadius="50"。
stroke:描邊
android:width="2dp" 描邊的寬度,android:color 描邊的顏色。
咱們還能夠把描邊弄成虛線的形式,設置方式爲:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。
corners:圓角
android:radius爲角的弧度,值越大角越圓。
咱們還能夠把四個角設定成不一樣的角度,方法爲:
<corners 
        android:topRightRadius="20dp"    右上角
        android:bottomLeftRadius="20dp"    右下角
        android:topLeftRadius="1dp"    左上角
        android:bottomRightRadius="0dp"    左下角
 />
這裏有個地方須要注意,bottomLeftRadius是右下角,而不是左下角,這個有點鬱悶,不過不影響使用,記得別搞錯了就行。
還有網上看到有人說設置成0dp無效,不過我在測試中發現是能夠的,我用的是2.2,可能修復了這個問題吧,若是無效的話那就只能設成1dp了。
padding:間隔

button_selector.xml:

[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <selector  

  3.     xmlns:android="http://schemas.android.com/apk/res/android">  

  4.     <item android:state_pressed="true" >  

  5.         <shape>  

  6.             <!-- 漸變 -->  

  7.             <gradient  

  8.                 android:startColor="#ff8c00"  

  9.                 android:endColor="#FFFFFF"  

  10.                 android:type="radial"  

  11.                 android:gradientRadius="50" />  

  12.             <!-- 描邊 -->  

  13.             <stroke  

  14.                 android:width="2dp"  

  15.                 android:color="#dcdcdc"  

  16.                 android:dashWidth="5dp"   

  17.                 android:dashGap="3dp" />  

  18.             <!-- 圓角 -->  

  19.             <corners  

  20.                 android:radius="2dp" />  

  21.             <padding  

  22.                 android:left="10dp"  

  23.                 android:top="10dp"  

  24.                 android:right="10dp"  

  25.                 android:bottom="10dp" />  

  26.         </shape>  

  27.     </item>  

  28.   

  29.     <item android:state_focused="true" >  

  30.         <shape>  

  31.             <gradient  

  32.                 android:startColor="#ffc2b7"  

  33.                 android:endColor="#ffc2b7"  

  34.                 android:angle="270" />  

  35.             <stroke  

  36.                 android:width="2dp"  

  37.                 android:color="#dcdcdc" />  

  38.             <corners  

  39.                 android:radius="2dp" />  

  40.             <padding  

  41.                 android:left="10dp"  

  42.                 android:top="10dp"  

  43.                 android:right="10dp"  

  44.                 android:bottom="10dp" />  

  45.         </shape>  

  46.     </item>  

  47.   

  48.     <item>         

  49.         <shape>  

  50.             <solid android:color="#ff9d77"/>  

  51.             <stroke  

  52.                 android:width="2dp"  

  53.                 android:color="#fad3cf" />  

  54.             <corners   

  55.                 android:topRightRadius="5dp"  

  56.                 android:bottomLeftRadius="5dp"  

  57.                 android:topLeftRadius="0dp"  

  58.                 android:bottomRightRadius="0dp"  

  59.             />  

  60.             <padding  

  61.                 android:left="10dp"  

  62.                 android:top="10dp"  

  63.                 android:right="10dp"  

  64.                 android:bottom="10dp" />  

  65.         </shape>  

  66.     </item>  

  67. </selector>  

運行效果以下圖:

通常狀態:


得到焦點狀態:


按下狀態:

相關文章
相關標籤/搜索