First, there are articles from: http://blog.sina.com.cn/s/blog_4b93170a0100qhwa.html
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.
button text effects:
Drawable/button_font.xml
1 <?xml version= "1.0" encoding= "Utf-8"?>2 <selector xmlns:android= "/http Schemas.android.com/apk/res/android ">3 <item android:state_selected=" true "android:color=" # FFF "/>4 <item android:state_focused=" true "android:color=" #FFF "/>5 <item Android:state_pressed= "true" android:color= "#FFF"/>6 <item android:color= "#000"/>7 </selector>
Button can also achieve more complex effects, such as gradients
Drawable/button_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_pressed= "true" >//defines the pattern when the button is in the pressed state. 4<shape>5 6<gradient android:startcolor= "#8600ff"/>7 8<stroke android:width= "2DP" android:color= "#000000"/>9<corners android:radius= "5DP"/>Ten<padding android:left= "10DP" android:top= "10DP" OneAndroid:bottom= "10DP" android:right= "10DP"/> A</shape> - -</item> the<item android:state_focused= "true" >//define the shape when the button gets 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"/> A</shape> at</item> -</selector>
Finally, you need to add two items to the XML file that contains the button . For example main.xml files that need to be in <button/> Riga two android:focusable= "true" android:background= "@drawable/button_color"
Add an example of Android registration button below:
1<TextView2Android:id= "@+id/register"3Android:clickable= "true"4Android:layout_width= "Wrap_content"5android:layout_height= "Wrap_content"6Android:layout_alignparentright= "true"7android:layout_marginright= "20dip"8android:layout_margintop= "15dip"9android:text= "@string/register"//<string name= "register" > Registration </string>TenAndroid:textcolor= "@color/text_color_selector" Oneandroid:background= "@drawable/textview_background" AAndroid:textsize= "16sp"/>
Res/color/text_color_selector.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=" true "android:color=" # Ffffff "/>4 <item android:state_pressed=" true "android:color=" #ffffff "/>5 < Item android:color= "#0099cc" ></item>6 </selector>
View Code
Res/drawable/textview_background.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >3<item android:state_pressed= "true" >4<shape>5<solid android:color= "#0099cc"/>6<stroke android:width= "1dip" android:color= "#0099cc"/>7<corners android:radius= "2DP"/>8<padding android:left= "2DP"9android:top= "2DP"Tenandroid:right= "2DP" Oneandroid:bottom= "2DP"/> A</shape> -</item> -<item android:state_focused= "true" > the<shape> -<solid android:color= "#0099cc"/> -<stroke android:width= "1dip" android:color= "#0099cc"/> -<corners android:radius= "2DP"/> +<padding android:left= "2DP" -android:top= "2DP" +android:right= "2DP" Aandroid:bottom= "2DP"/> at</shape> -</item> -<item> -<shape> -<solid android:color= "#00000000"/> -<stroke android:width= "1dip" android:color= "#0099cc"/> in<corners android:radius= "2DP"/> -<padding android:left= "2DP" toandroid:top= "2DP" +android:right= "2DP" -android:bottom= "2DP"/> the</shape> *</item> $</selector>
View Code
Android Registration button-Background selector selector