您好,登錄后才能下訂單哦!
可視化編輯器,alt+1到9鍵,切換到工具區(qū),tab鍵,選擇按鈕,esc鍵,返回編輯
Android使用selector自定義按鈕
以前定義按鈕的時(shí)候,想使用自定義的Button總是習(xí)慣在activity中獲取該按鈕然后,重寫該按鈕的onTouchListener然后在當(dāng)中切換按鈕的背景圖片。這樣做不但使activity中的代碼變得臃腫而且不能該按鈕不能達(dá)到重用的目的。
后來接觸到了Android 中的selector挺好用的,下面詳細(xì)講解一下如何使用selector,首先在res\drawable文件夾下創(chuàng)建一個(gè)xml文件如圖:
再在drawable文件中添加你想應(yīng)用在按鈕上的圖片,包括按下的圖片和不按時(shí)的圖片。
然后在該文件中添加:drawable
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <!--當(dāng)按鈕獲得焦點(diǎn)時(shí)定義顯示的圖片--> <item android:state_focused = "true" android:drawable = "@drawable/p_w_picpath_btn_down"/> <!--當(dāng)按鈕被選中時(shí)定義現(xiàn)實(shí)的圖片 --> <item android:state_selected = "true" android:drawable = "@drawable/p_w_picpath_btn_down"/> <!--當(dāng)按鈕被點(diǎn)擊時(shí)定義顯示的圖片--> <item android:state_pressed = "true" android:drawable = "@drawable/p_w_picpath_btn_down"/> <!--默認(rèn)按鈕沒有被選中時(shí)顯示的圖片--> <item android:drawable = "@drawable/p_w_picpath_btn_up" /> </selector>
該文件名字叫做button_selector.xml,完成之后,把你想應(yīng)用的按鈕的drawable屬性改為@drawable/button_selector就可以了 如下:
<Button android:id="@+id/button_add" android:layout_width="115dp" android:layout_height="wrap_content" android:layout_margin="5px" android:layout_weight="1" android:background="@drawable/button_selector" android:text="自定義按鈕" />
開發(fā)微博推薦: http://crh.chinacloudsites.cn/
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。