Android開發(fā)手冊(cè)Button實(shí)現(xiàn)selector選擇器
selector是按鈕最常用的功能,對(duì)美化控件的作用很大。
上節(jié)我們說了selector和shape聯(lián)合使用,但偏向shape的介紹,今天主要說selector。
??實(shí)踐過程
我們先按照上一節(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)行起來看看,哎,效果很正確啊
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)文章
安卓應(yīng)用開發(fā)通過java調(diào)用c++ jni的圖文使用方法
這篇文章主要介紹了2013-11-11
Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果
這篇文章主要為大家介紹了Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼
這篇文章主要介紹了TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼,可實(shí)現(xiàn)左右滑動(dòng)切換視圖界面和點(diǎn)擊切換,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01
Android中不同狀態(tài)頁面管理優(yōu)化技巧詳解
在Android中,不管是activity或者fragment,在加載視圖的時(shí)候都有可能會(huì)出現(xiàn)多種不同的狀態(tài)頁面View,所以本文就來聊聊Android中不同狀態(tài)頁面管理優(yōu)化吧2024-04-04
淺談Android studio 生成apk文件時(shí)的 key store path 的問題
這篇文章主要介紹了淺談Android studio 生成apk文件時(shí)的 key store path 的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
GridView基于pulltorefresh實(shí)現(xiàn)下拉刷新 上拉加載更多功能(推薦)
原理和listview一樣 ,都是重寫Android原生控件。下面小編通過實(shí)例代碼給大家分享GridView基于pulltorefresh實(shí)現(xiàn)下拉刷新 上拉加載更多功能,非常不錯(cuò),一起看看吧2016-11-11

