A Create XML file, location:drawable/xxx.xml, in the same directory remember to put related pictures
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >&NBSP;
<!-- Background image by default -->
<item android:drawable= " @drawable/pic1 "/>&NBSP;&NBSP;&NBSP;&NBSP;
<!-- background picture when there is no focus -->
<item android:state_window_focused= " False "&NBSP;&NBSP;&NBSP;
android:drawable=" @drawable/pic1 "/ >
<!-- background picture when I get focus in non-touch mode and click --> &NBSP;&NBSP
<item android:state_focused= "true" android:state_pressed= "true" android:drawable= "@drawable/pic2"/>&NBSP;
<!--the background picture when clicked in touch mode --
<item android:state_focused= "false" android:state_pressed= "true" android:drawable= "@drawable/pic3"/>
<!--picture background When selected- -
<item android:state_selected= "true" android:drawable= "@drawable/pic4"/>
<!-- picture background when getting focus -
<item android:state_focused= "true" android:drawable= "@drawable/pic5"/>
</selector>
two . Use XML file:
1. Method One: Configure the android:listselector= "@drawable/xxx in the ListView
Or add properties to the ListView item android:background= "@drawable/xxx"
2. Method Two:drawable drawable = Getresources (). getdrawable (R.DRAWABLE.XXX);
Listview.setselector (drawable); but this will appear the list sometimes black, need to add:android:cachecolorhint= "@android: Color/transparent" Make it transparent.
Related properties:
Android:state_selected is selected
Android:state_focused is gaining focus
Android:state_pressed is click
Android:state_enabled is the set response event , which refers to all events
The selector effect of the button can also be set according to these states . You can also set selector to change The state of text in a button.
The following is the text effect in the configuration button:
Drawable/button_font.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_selected= "true" android:color= "#FFF"/>
<item android:state_focused= "true" android:color= "#FFF"/>
<item android:state_pressed= "true" android:color= "#FFF"/>
<item android:color= "#000"/>
</selector>
ButtonYou can also achieve more complex effects, such as gradients
Drawable/button_color.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >/
<item android:state_pressed= "True" >// defines the pattern when the button is in the pressed state.
<shape>
<gradient android:startcolor= "#8600ff"/>
<stroke android:width= "2DP" android:color= "#000000"/>
<corners android:radius= "5DP"/>
<padding android:left= "10DP" android:top= "10DP"
android:bottom= "10DP" android:right= "10DP"/>
</shape>
</item>
<item android:state_focused= "true" >// Define whenButton getsThe shape of focus
<shape>
<gradient android:startcolor= "#eac100"/>
<stroke android:width= "2DP" android:color= "#333333" color= "#ffffff"/>
<corners android:radius= "8DP"/>
<padding android:left= "10DP" android:top= "10DP"
Android:bottom= "10DP" android:right= "10DP"/>
</shape>
</item>
</selector>
Finally, you need to includeadd two items to the button's XML file. For example main.xml files, you need to add two Android in <button/> : focusable= "true" android:background= "@drawable/button_color"
XML selector Change the click of the Interface status button and the normal background toggle color of the click and Normal toggle