基于android背景選擇器selector的用法匯總
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 沒(méi)有焦點(diǎn)時(shí)的背景圖片 -->
<item android:drawable="@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/>
<!-- 觸摸模式按下時(shí)的背景圖片 -->
<item android:drawable="@drawable/handle_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/handle_normal" android:state_enabled="true"/>
<!-- 獲得焦點(diǎn)時(shí)的圖片背景 -->
<item android:drawable="@drawable/handle_focused" android:state_focused="true"/>
<!--
android:state_selected是選中
android:state_focused是獲得焦點(diǎn)
android:state_pressed是點(diǎn)擊
android:state_enabled是設(shè)置是否響應(yīng)事件,指所有事件
-->
</selector>
二.使用xml文件:
1.方法一:在listview中配置android:listSelector="@drawable/xxx
或者在listview的item中添加屬性android:background="@drawable/xxx"
2.方法二:Drawable drawable = getResources().getDrawable(R.drawable.xxx);
ListView.setSelector(drawable);
相關(guān)屬性:
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
<?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ù)雜的效果,例如漸變
drawable/button_color.xml
<SPAN style="COLOR: #330033"><?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></SPAN><SPAN style="COLOR: #ff0000"> </SPAN>
最后,需要在包含 button的xml文件里添加兩項(xiàng)。例如main.xml 文件,需要在<Button />里加兩項(xiàng)android:focusable="true" android:background="@drawable/button_color"這樣就把drawale/button_color.xml文件與這個(gè)控件關(guān)聯(lián)起來(lái)了。
- Android selector背景選擇器的使用詳解
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- 淺談?wù)凙ndroid 圖片選擇器
- Android自定義可循環(huán)的滾動(dòng)選擇器CycleWheelView
- android 字體顏色選擇器(ColorPicker)介紹
- Android仿微信照片選擇器實(shí)現(xiàn)預(yù)覽查看圖片
- Android開(kāi)發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)
- Android中顏色選擇器和改變字體顏色的實(shí)例教程
- Android PickerView滾動(dòng)選擇器的使用方法
- android自定義滾軸選擇器
相關(guān)文章
Android進(jìn)階Handler應(yīng)用線(xiàn)上卡頓監(jiān)控詳解
這篇文章主要為大家介紹了Android進(jìn)階Handler應(yīng)用線(xiàn)上卡頓監(jiān)控詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
設(shè)備APP開(kāi)發(fā)環(huán)境配置細(xì)節(jié)介紹
隨著工業(yè)自動(dòng)化的不斷發(fā)展,設(shè)備APP也越來(lái)越重要,本文就設(shè)備APP開(kāi)發(fā)軟件配置細(xì)節(jié)做一個(gè)深入詳解2022-09-09
Android HttpURLConnection下載網(wǎng)絡(luò)圖片設(shè)置系統(tǒng)壁紙
這篇文章主要為大家詳細(xì)介紹了Android HttpURLConnection下載網(wǎng)絡(luò)圖片設(shè)置系統(tǒng)壁紙,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android圖片無(wú)限輪播的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android圖片無(wú)限輪播的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼
這篇文章主要介紹了android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android 中ListView點(diǎn)擊Item無(wú)響應(yīng)問(wèn)題的解決辦法
如果listitem里面包括button或者checkbox等控件,默認(rèn)情況下listitem會(huì)失去焦點(diǎn),導(dǎo)致無(wú)法響應(yīng)item的事件,怎么解決呢?下面小編給大家分享下listview點(diǎn)擊item無(wú)響應(yīng)的解決辦法2016-12-12
Android實(shí)現(xiàn)微信首頁(yè)左右滑動(dòng)切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)微信首頁(yè)左右滑動(dòng)切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

