淺談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 version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 預設時的背景圖片 --><item android:drawable="@drawable/pic1" /><!-- 沒有焦點時的背景圖片 --><item android:state_window_focused="false" android:drawable="@drawable/pic1" /><!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 --><item android:state_focused="true" android:state_pressed="true"android:drawable="@drawable/pic2" /><!-- 觸摸模式下單擊時的背景圖片 --><item android:state_focused="false" android:state_pressed="true"android:drawable="@drawable/pic3" /><!--選中時的圖片背景 --><item android:state_selected="true" android:drawable="@drawable/pic4" /><!--獲得焦點時的圖片背景 --><item android:state_focused="true" android:drawable="@drawable/pic5" /></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.xml

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

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

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true"><!-- 定義當button 處於pressed 狀態時的形態。 --><shape><gradient android:startColor="#8600ff" /><stroke android:width="2dp" android:color="#000000" /><corners android:radius="5dp" /><padding android:left="10dp" android:top="10dp"android:bottom="10dp" android:right="10dp" /></shape></item><item android:state_focused="true"><!-- 定義當button獲得 focus時的形態 --><shape><gradient android:startColor="#eac100" /><stroke android:width="2dp" android:color="#333333" color="#ffffff" /><corners android:radius="8dp" /><padding android:left="10dp" android:top="10dp"android:bottom="10dp" android:right="10dp" /></shape></item></selector>

 

  最後,需要在包含 button的xml檔案裡添加兩項。假如是 main.xml 檔案,我們需要在<Button />裡加兩項。 

     android:focusable="true" 
     android:backgroud="@drawable/button_color"
這樣當你使用Button的時候就可以甩掉系統內建的那黃顏色的背景了,實現個人化的背景,配合應用的整體布局非常之有用啊。

轉自:http://blog.csdn.net/jrcisme/article/details/6444653

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.