Android開發(fā)手冊(cè)Button實(shí)現(xiàn)selector選擇器
selector是按鈕最常用的功能,對(duì)美化控件的作用很大。
上節(jié)我們說(shuō)了selector和shape聯(lián)合使用,但偏向shape的介紹,今天主要說(shuō)selector。
??實(shí)踐過(guò)程
我們先按照上一節(jié)的shape方式創(chuàng)建兩個(gè)shape背景
btn_selector_shape1.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 圓角 --> <corners android:radius="5dp" /> <!--填充顏色--> <solid android:color="#00ff00" /> </shape>
btn_selector_shape2.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--圓角--> <corners android:radius="5dp" /> <!--填充顏色--> <solid android:color="#0000ff" /> </shape>
接著我們?cè)凇緍es-drawable】右鍵創(chuàng)建個(gè)Drawable Resource File ,彈出框?qū)懳募麆?chuàng)建文件,設(shè)置默認(rèn)【Root element】為selector。
btn_selector0.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" /> <item android:drawable="@drawable/btn_selector_shape2" android:state_window_focused="false" /> </selector>
布局中引用
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" tools:context=".TextActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="160dp" android:background="@drawable/btn_selector0" android:text="按下變色" android:textColor="@color/white" /> </RelativeLayout>
我們運(yùn)行下看看
但是
我們回憶下,剛才是不是創(chuàng)建了三個(gè)文件,按鈕少的情況下還好,自定義的按鈕一多,這么多文件非常不容易管理,所以我們要用另外一種寫法,將所有內(nèi)容放到一個(gè)文件中。
我們?cè)趧偛诺腷tn.selector0.xml中修改:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--這是第一種方式,利用drwable引用文件--> <!--<item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />--> <!--<item android:drawable="@drawable/btn_selector_shape2" android:state_pressed="false" />--> <!--第二種方式如下--> <item android:state_pressed="false"> <shape android:shape="rectangle"> <!-- 圓角 --> <corners android:radius="5dp" /> <!--填充顏色為白色--> <solid android:color="#0000ff" /> </shape> </item> <!--單擊時(shí)是一個(gè)帶圓角,白色背景,綠色邊框的矩形--> <item android:state_pressed="true"> <shape android:shape="rectangle"> <!--圓角--> <corners android:radius="5dp" /> <!--填充顏色為白色--> <solid android:color="#00ff00" /> </shape> </item> </selector>
我們運(yùn)行起來(lái)看看,哎,效果很正確啊
Selector的屬性不止這兩個(gè)哦:
- state_focused 布爾值,是否獲得焦點(diǎn)
- state_window_focused 布爾值,是否獲得窗口焦點(diǎn)
- state_enabled 布爾值,控件是否可用
- state_checkable 布爾值,控件可否被勾選
- state_checked 布爾值,控件是否被勾選
- state_selected 布爾值,控件是否被選擇,針對(duì)有滾輪的情況
- state_pressed 布爾值,控件是否被按下
- state_active 布爾值,控件是否處于活動(dòng)狀態(tài)
- state_single和state_first和state_middle很少使用,知道就行
以上就是Android開發(fā)手冊(cè)Button實(shí)現(xiàn)selector選擇器的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)Button selector選擇器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android開發(fā)雙向滑動(dòng)選擇器范圍SeekBar實(shí)現(xiàn)
- Android中的TimePickerView(時(shí)間選擇器)的用法詳解
- Android?studio實(shí)現(xiàn)日期?、時(shí)間選擇器與進(jìn)度條
- Android PicSelector圖片選擇器小功能
- Android實(shí)現(xiàn)拼多多地址選擇器
- Android如何實(shí)現(xiàn)年月選擇器功能
- Android實(shí)現(xiàn)四級(jí)聯(lián)動(dòng)地址選擇器
- Android自定義酒店日期選擇器
- Jetpack Compose之選擇器使用實(shí)例講解
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)帶有反彈效果仿IOS反彈scrollview教程詳解
本文給大家分享android開發(fā)實(shí)現(xiàn)帶有反彈效果,模仿ios反彈scrollview詳細(xì)教程,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android Studio 視頻播放失敗 start called in state1 異常怎么解決
很多朋友問(wèn)小編在使用MediaPlayer播放音頻時(shí)報(bào)出 E/MediaPlayerNative: start called in state 1, mPlayer(0x0)問(wèn)題,該如何處理呢,今天小編給大家?guī)?lái)了Android Studio 視頻播放失敗 start called in state1 異常問(wèn)題,需要的朋友可以參考下2020-03-03Android中TextView局部變色功能實(shí)現(xiàn)
這篇文章給大家詳細(xì)講解了一下Android中TextView實(shí)現(xiàn)部分文字不同顏色的功能實(shí)現(xiàn)過(guò)程,有這方面需要的朋友們一起學(xué)習(xí)下吧。2017-12-12Android實(shí)現(xiàn)環(huán)形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)環(huán)形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android Shader應(yīng)用開發(fā)之霓虹閃爍文字效果
這篇文章主要為大家詳細(xì)介紹了Android Shader應(yīng)用開發(fā)之霓虹閃爍文字效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07解決Error:All flavors must now belong to a named flavor dimens
這篇文章主要介紹了解決Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com,需要的朋友可以參考下2017-11-11詳解Android 視頻播放時(shí)停止后臺(tái)運(yùn)行的方法
這篇文章主要介紹了詳解Android 視頻播放時(shí)停止后臺(tái)運(yùn)行的方法的相關(guān)資料,需要的朋友可以參考下2017-06-06Android TextView和ImageView簡(jiǎn)單說(shuō)明
Android TextView和ImageView簡(jiǎn)單說(shuō)明,需要的朋友可以參考一下2013-03-03