Android中的Selector
最近做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代碼:
- <?xml version="1.0" encoding="utf-8" ?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 默認(rèn)時(shí)的背景圖片 -->
- <item android:drawable="@drawable/pic1" />
- <!-- 沒(méi)有焦點(diǎn)時(shí)的背景圖片 -->
- <item android:state_window_focused="false"
- android:drawable="@drawable/pic1" />
- <!-- 非觸摸模式下獲得焦點(diǎn)并單擊時(shí)的背景圖片 -->
- <item android:state_focused="true" android:state_pressed="true"
- android:drawable= "@drawable/pic2" />
- <!-- 觸摸模式下單擊時(shí)的背景圖片 -->
- <item android:state_focused="false" android:state_pressed="true"
- android:drawable="@drawable/pic3" />
- <!--選中時(shí)的圖片背景 -->
- <item android:state_selected="true"
- android:drawable="@drawable/pic4" />
- <!--獲得焦點(diǎn)時(shí)的圖片背景 -->
- <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"即可實(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代碼:
- <?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還可以實(shí)現(xiàn)更復(fù)雜的效果,例如漸變啊等等。
Java代碼:
- 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">//定義當(dāng)button 處于pressed 狀態(tài)時(shí)的形態(tài)。
- <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">//定義當(dāng)button獲得 focus時(shí)的形態(tài)
- <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文件里添加兩項(xiàng)。假如是 main.xml 文件,我們需要在<Button />里加兩項(xiàng)。
- android:focusable="true"
- android:backgroud="@drawable/button_color"
這樣當(dāng)你使用Button的時(shí)候就可以甩掉系統(tǒng)自帶的那黃顏色的背景了,實(shí)現(xiàn)個(gè)性化的背景,配合應(yīng)用的整體布局非常之有用啊。
【編輯推薦】