自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android中的Selector

移動(dòng)開(kāi)發(fā) Android
Android Selector 是一種背景選擇器,也可以用來(lái)更改界面狀態(tài)。總之,可以改變你想要的特效。

最近做listview和button都要改變Android原來(lái)控件的背景,在網(wǎng)上查找了一些資料不是很全,所以現(xiàn)在總結(jié)一下android的selector的用法。

首先android的selector是在drawable/xxx.xml中配置的。

先看一下listview中的狀態(tài):

把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系統(tǒng)使用時(shí)根據(jù)ListView中的列表項(xiàng)的狀態(tài)來(lái)使用相應(yīng)的背景圖片。drawable/list_item_bg.xml

Java代碼:

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <!-- 默認(rèn)時(shí)的背景圖片 -->   
  4. <item android:drawable="@drawable/pic1" />   
  5. <!-- 沒(méi)有焦點(diǎn)時(shí)的背景圖片 -->   
  6. <item android:state_window_focused="false"   
  7. android:drawable="@drawable/pic1" />   
  8. <!-- 非觸摸模式下獲得焦點(diǎn)并單擊時(shí)的背景圖片 -->   
  9. <item android:state_focused="true" android:state_pressed="true"   
  10. android:drawable"@drawable/pic2" />   
  11. <!-- 觸摸模式下單擊時(shí)的背景圖片 -->   
  12. <item android:state_focused="false" android:state_pressed="true"   
  13. android:drawable="@drawable/pic3" />   
  14. <!--選中時(shí)的圖片背景 -->   
  15. <item android:state_selected="true"   
  16. android:drawable="@drawable/pic4" />   
  17. <!--獲得焦點(diǎn)時(shí)的圖片背景 -->   
  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"即可實(shí)現(xiàn),或者在Java代碼中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);  ListView.setSelector(drawable);同樣的效果。

但是這樣會(huì)出現(xiàn)列表有時(shí)候?yàn)楹诘那闆r,需要加上:android:cacheColorHint="@android:color/transparent"

使其透明。

其次再來(lái)看看Button的一些背景效果:

android:state_selected是選中

android:state_focused是獲得焦點(diǎn)

android:state_pressed是點(diǎn)擊

android:state_enabled是設(shè)置是否響應(yīng)事件,指所有事件

根據(jù)這些狀態(tài)同樣可以設(shè)置button的selector效果。也可以設(shè)置selector改變button中的文字狀態(tài)。以下就是配置button中的文字效果:drawable/button_font.xml

Java代碼:

  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還可以實(shí)現(xiàn)更復(fù)雜的效果,例如漸變啊等等。

Java代碼:

  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">//定義當(dāng)button 處于pressed 狀態(tài)時(shí)的形態(tài)。   
  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">//定義當(dāng)button獲得 focus時(shí)的形態(tài)   
  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文件里添加兩項(xiàng)。假如是 main.xml 文件,我們需要在<Button />里加兩項(xiàng)。

  1. android:focusable="true" 
  2. android:backgroud="@drawable/button_color" 

這樣當(dāng)你使用Button的時(shí)候就可以甩掉系統(tǒng)自帶的那黃顏色的背景了,實(shí)現(xiàn)個(gè)性化的背景,配合應(yīng)用的整體布局非常之有用啊。

【編輯推薦】

Android 多任務(wù)多線程斷點(diǎn)下載

 Android Phone 7界面設(shè)置小教程

Android Activity和Intent機(jī)制學(xué)習(xí)筆記

Android UI控件組合應(yīng)用之二:按鈕布局

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2021-03-25 09:58:22

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2016-06-02 15:10:12

SwiftSelector

2025-02-28 09:14:09

JavaNIO機(jī)制

2022-02-22 08:00:48

JavaNIOBuffer

2011-07-26 10:50:50

Objective-C Selector

2011-12-12 10:33:47

JavaNIO

2011-12-12 10:19:00

JavaNIO

2011-02-16 09:57:41

2013-05-31 17:14:40

情感設(shè)計(jì)Android Des

2011-06-01 13:22:25

Android Alarm

2019-10-16 16:31:59

權(quán)限Android程序

2011-05-27 11:34:53

Android preference

2013-03-28 09:07:37

Android開(kāi)發(fā)Intent機(jī)制

2014-08-07 10:06:05

ndroidContext

2012-05-18 12:49:43

Android

2017-05-15 19:40:40

AndroidIPC機(jī)制

2009-04-03 08:21:37

AndroidGoogle移動(dòng)OS

2010-01-27 10:07:11

Android adb

2018-07-30 11:56:17

解密加密開(kāi)發(fā)

2013-03-28 16:31:48

AIDL的簡(jiǎn)單使用Android中AID
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)