1、圖片設置背景選擇器,以便點按或設置選中與否,背景切換
res/drawable/selector_settings_item_back.xml
1
2
3
4
5
6
7
|
<?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
1
2
3
4
5
|
<?xml version= "1.0" encoding= "utf-8" ?> <resources> <color name= "settingsItem" >#ffffff</color> <color name= "settingsSelectedItem" >#FFA500</color> </resources> |
2、圓角按鈕,按下抬起切換背景,同時切換文字顏色
res/layout/activity_xxx.xml
1
2
3
4
5
6
7
8
|
<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/ 下的兩個 selector ,
一個是背景圖片隨點按抬起狀態進行切換,一個是文本顏色隨點按抬起進行切換。
res/drawable/selector_shape_corner_button.xml
1
2
3
4
5
6
7
8
|
<?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
1
2
3
4
5
6
7
8
9
10
11
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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 中圖片和按鈕按下狀態變化實例代碼解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/opengl_es/article/details/80558381