Create the Color.xml file in the values directory first
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <drawable name= "Red" > #f00 </ drawable> <drawable name= "green" > #0f0 </drawable> <drawable name= "Gray" > #ccc </ Drawable>
Create a new drawable directory under RES and create a new btn_bg.xml and Btn_color.xml file with the following code:
Btn_bg.xml
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <selector xmlns:android= "/http Schemas.android.com/apk/res/android "> 3 <item android:state_window_focused=" false " Android:state_enabled= "true" 4 android:drawable= "@drawable/btn_test_normal"/> 5 <item android:state_enabled= "false" android:drawable= "@drawable/btn_test_normal"/> 6 <item android:state_pressed= "true" android:drawable= "@drawable/btn_test_press"/> 7 <item android:state_focused= "true"8 android:drawable= "@drawable/btn_test_normal"/> 9
Btn_color.xml
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <selector xmlns:android= "/http Schemas.android.com/apk/res/android "> 3 <item android:state_focused=" false "android:state _enabled= "true" android:state_pressed= "false" 4 android:color= "@drawable/red"/> 5 <item android:state_enabled= "false" android:color= "@drawable/gray"/> 6 <item android:state_pressed= "true" android:color= "@drawable/green"/> 7 <item Android:state_focused= "true" android:color= "@drawable/red"/> 8 </selector>
Using in layouts
1 <button 2 android:layout_width= "Wrap_content" 3 android:layout_height= "Wrap_content" 4 android:text= "Press text will change effect" 5 Android:textcolor= "@drawable/btn_color" 6 android:background= "@drawable/btn_bg" 7 />
Click button font color, picture change, background change