android selector 開始自定義樣式

Selector的結構描述:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:color="hex_color"
            android:state_pressed="true/false"
<!-- “true”表示按下狀態使用(例如按鈕按下);“false”表示非按下狀態使用。 -->
            android:state_focused="true/false"
<!-- “true”表示聚焦狀態使用(例如使用滾動球/D-pad聚焦Button);“false”表示非聚焦狀態使用。 -->
            android:state_selected="true/false"
<!-- “true”表示選中狀態使用(例如Tab打開);“false”表示非選中狀態使用。 -->
            android:state_active="true/false"
<!-- “true”表示可勾選狀態時使用;“false”表示非可勾選狀態使用。(只對能切換可勾選—非可勾選的構件有用。) -->
            android:state_checkable="true/false"
<!--  “true”表示勾選狀態使用;“false”表示非勾選狀態使用。 -->
            android:state_checked="true/false"
<!-- true”表示勾選狀態使用;“false”表示非勾選狀態使用。 -->
            android:state_enabled="true/false"
<!-- “true”表示可用狀態使用(能接收觸摸/點擊事件);“false”表示不可用狀態使用。 -->
            android:state_window_focused="true/false"
<!-- “true”表示應用程序窗口有焦點時使用(應用程序在前臺);“false”表示無焦點時使用(例如Notification欄拉下或對話框顯示)。 -->
		/>
 </selector>
shape的結構描述:

 <shape>  
   <!-- 實心 -->  
   <solid android:color="#ff9d77"/>  
   <!-- 漸變 -->  
   <gradient  
       android:startColor="#ff8c00"  <!—開始顏色 -->  
       android:endColor="#FFFFFF"  <!—結束顏色 -->  
       android:angle="270" />  
   <!-- 描邊 -->  
   <stroke  
       android:width="2dp"  
       android:color="#dcdcdc" />  
   <!-- 圓角 -->  
   <corners  
       android:radius="2dp" />  
   <padding  
       android:left="10dp"  
       android:top="10dp"  
       android:right="10dp"  
       android:bottom="10dp" />  
</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:間隔
這個就不用多說了,XML佈局文件中經常用到。
具體代碼如下:
main.xml: 

<Button  
  android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:text="TestShapeButton"  
 android:background="@drawable/button_selector"   />  
button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>  
<selector  
   xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_pressed="true" >  
       <shape>  
           <!-- 漸變 -->  
          <gradient  
               android:startColor="#ff8c00"  
               android:endColor="#FFFFFF"  
               android:type="radial"  
               android:gradientRadius="50" />  
           <!-- 描邊 -->  
           <stroke  
               android:width="2dp"  
               android:color="#dcdcdc"  
               android:dashWidth="5dp"  
               android:dashGap="3dp" />  
           <!-- 圓角 -->  
           <corners  
               android:radius="2dp" />  
           <padding  
               android:left="10dp"  
               android:top="10dp"  
               android:right="10dp"  
               android:bottom="10dp" />  
       </shape>  
   </item>  
   <item android:state_focused="true" >  
       <shape>  
           <gradient  
               android:startColor="#ffc2b7"  
               android:endColor="#ffc2b7"  
               android:angle="270" />  
           <stroke  
               android:width="2dp"  
               android:color="#dcdcdc" />  
           <corners  
               android:radius="2dp" />  
           <padding  
               android:left="10dp"  
               android:top="10dp"  
               android:right="10dp"  
               android:bottom="10dp" />  
       </shape>  
   </item>  
   <item>        
       <shape>  
           <solid android:color="#ff9d77"/>  
           <stroke  
               android:width="2dp"  
               android:color="#fad3cf" />  
           <corners  
              android:topRightRadius="5dp"  
               android:bottomLeftRadius="5dp"  
               android:topLeftRadius="0dp"  
               android:bottomRightRadius="0dp"  />  
           <padding  
               android:left="10dp"  
               android:top="10dp"  
               android:right="10dp"  
               android:bottom="10dp" />  
       </shape>  
   </item>  
</selector> 

轉自點擊打開鏈接



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章