Selector與Shape的基本用法

1. Selector


drawable 的 item 中可以有以下屬性:

android:drawable ="@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"]

 

2. Shape


                solid:實心,就是填充     

android:color = "#000000"   指定填充的顏色


                gradient:漸變

android:startColor  起始顏色 
android:endColor    結束顏色 
android:angle       漸變角度,必須爲45的整數倍。 
                      
漸變模式: 
android:type="linear"       默認爲線性漸變模式 
android:type="radial"       徑向漸變,需要指定半徑 
android:gradientRadius="50" 半徑爲50


                stroke:描邊

android:width="2dp"描邊的寬度 
android:color       描邊的顏色 
                   
還可以把描邊弄成虛線的形式,設置方式爲: 
android:dashWidth="5dp"表示'-'這樣一個橫線的寬度 
android:dashGap="3dp"  表示'-'之間隔開的距離


                corners:圓角  

android:radius  角的弧度,值越大角越圓 
                 
還可以把四個角設定成不同的角度: 
    <corners  
       android:topRightRadius="20dp"      右上角 
        android:bottomLeftRadius="20dp"   右下角 
        android:topLeftRadius="1dp"       左上角 
        android:bottomRightRadius="0dp"   左下角 
     />


        這裏有個地方需要注意,bottomLeftRadius是右下角,而不是左下角,這個有點鬱悶,不過不影響使用,記得別搞錯了就行。


               padding:間隔

 

3. 用法:


第一種是在 listview 中配置:

android:listSelector ="@drawable/list_item_bg"

第二種是在listview的item中添加屬性:


android:background ="@drawable/list_item_bg"

第三種是在Java代碼中使用:


Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); 
listView.setSelector(drawable);

 

4. 例:list_item_bg.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>



 

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