android的selector,背景選取器

來源:互聯網
上載者:User

最近做listview和button都要改變Android原來控制項的背景,在網上尋找了一些資料不是很全,所以現在總結一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的狀態:
把下面的XML檔案儲存成你自己命名的.xml檔案(比如list_item_bg.xml),在系統使用時根據ListView中的清單項目的狀態來使用相應的背景圖片。
drawable/list_item_bg.xml

XML/HTML代碼
  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"     
  10.         android:drawable= "@drawable/pic2" />    
  11. <!-- 觸摸模式下單擊時的背景圖片 -->    
  12.   <item android:state_focused="false" android:state_pressed="true"     
  13.         android:drawable="@drawable/pic3" />     
  14. <!--選中時的圖片背景  -->    
  15.   <item android:state_selected="true"     
  16.         android:drawable="@drawable/pic4" />     
  17. <!--獲得焦點時的圖片背景  -->    
  18.   <item android:state_focused="true"     
  19.         android:drawable="@drawable/pic5" />     
  20. </selector>  

使用些xml檔案:第一種是在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);同樣的效果。
但是這樣會出現列表有時候為黑的情況,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。
其次再來看看Button的一些背景效果:
android:state_selected是選中
android:state_focused是獲得焦點
android:state_pressed是點擊
android:state_enabled是設定是否響應事件,指所有事件
根據這些狀態同樣可以設定button的selector效果。也可以設定selector改變button中的文字狀態。
以下就是配置button中的文字效果:
drawable/button_font.xmlXML/HTML代碼

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_selected="true" android:color="#FFF" />  
  4.     <item android:state_focused="true" android:color="#FFF" />  
  5.     <item android:state_pressed="true" android:color="#FFF" />  
  6.     <item android:color="#000" />  
  7. </selector>  

Button還可以實現更複雜的效果,例如漸層啊等等。

XML/HTML代碼
  1. drawable/button_color.xml  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">         /   
  4.         <item android:state_pressed="true">//定義當button 處於pressed 狀態時的形態。   
  5.               <shape>   
  6.                      <gradient  android:startColor="#8600ff" />   
  7.                       <stroke   android:width="2dp" android:color="#000000" />   
  8.                        <corners android:radius="5dp" />    
  9.                        <padding android:left="10dp" android:top="10dp"   
  10.                                 android:bottom="10dp" android:right="10dp"/>    
  11.                  </shape>   
  12.         </item>   
  13.         <item android:state_focused="true">//定義當button獲得 focus時的形態   
  14.                  <shape>   
  15.                        <gradient android:startColor="#eac100"/>   
  16.                         <stroke android:width="2dp" android:color="#333333"  color="#ffffff"/>   
  17.                          <corners android:radius="8dp" />     
  18.                          <padding android:left="10dp" android:top="10dp"   
  19.                                   android:bottom="10dp" android:right="10dp"/>                     
  20.                 </shape>   
  21.             </item>  
  22. </selector>   

     最後,需要在包含 button的xml檔案裡添加兩項。假如是 main.xml 檔案,
我們需要在<Button />裡加兩項。 
     android:focusable="true" 
     android:backgroud="@drawable/button_color"
這樣當你使用Button的時候就可以甩掉系統內建的那黃顏色的背景了,實現個人化的背景,配合應用的整體布局非常之有用啊。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.