您好,登錄后才能下訂單哦!
這篇文章主要介紹Android中圖片和按鈕按下?tīng)顟B(tài)變化的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1、圖片設(shè)置背景選擇器,以便點(diǎn)按或設(shè)置選中與否,背景切換
res/drawable/selector_settings_item_back.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@color/settingsSelectedItem"/> <item android:state_pressed="true" android:drawable="@color/settingsSelectedItem"/> <item android:state_selected="true" android:drawable="@color/settingsSelectedItem"/> <item android:state_focused="false" android:drawable="@color/settingsItem"/> </selector >
顏色值定義:
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="settingsItem">#ffffff</color> <color name="settingsSelectedItem">#FFA500</color> </resources>
2、圓角按鈕,按下抬起切換背景,同時(shí)切換文字顏色
res/layout/activity_xxx.xml
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/selector_shape_corner_button" android:text="審批中" android:textColor="@drawable/selector_font_style_corner_button" android:textSize="13sp" />
其中引用了 res/drawable/ 下的兩個(gè) selector ,
一個(gè)是背景圖片隨點(diǎn)按抬起狀態(tài)進(jìn)行切換,一個(gè)是文本顏色隨點(diǎn)按抬起進(jìn)行切換。
res/drawable/selector_shape_corner_button.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@drawable/shape_corner_button_fill"/> <item android:state_pressed="true" android:drawable="@drawable/shape_corner_button_fill"/> <item android:state_selected="true" android:drawable="@drawable/shape_corner_button_fill"/> <item android:state_focused="false" android:drawable="@drawable/shape_corner_button"/> </selector > res/drawable/selector_font_style_corner_button
shape_corner_button.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="#001da1f2" /> <stroke android:width="1dp" android:color="#1da1f2" /> </shape>
shape_corner_button_fill.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="#ff1da1f2" /> <stroke android:width="1dp" android:color="#1da1f2" /> </shape> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="#ffffff"/> <item android:state_pressed="true" android:color="#ffffff"/> <item android:state_selected="true" android:color="#ffffff"/> <item android:state_focused="false" android:color="#1da1f2"/> </selector >
以上是“Android中圖片和按鈕按下?tīng)顟B(tài)變化的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。