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 > <!--default background picture--<item android:drawable= "@drawable/pic1"/> <!--background picture without focus--<item Android:state_window_focused= "false" android:drawable= "@drawable/pic1"/> <!--get focus in non-touch mode and click Background Picture-< Item android:state_focused= "True" android:state_pressed= "true" android:drawable= "@drawable/pic2"/> <!-- Background picture when clicked in touch mode--<item android:state_focused= "false" android:state_pressed= "true" android:drawable= "@ Drawable/pic3 "/> <!--selected picture background--<item android:state_selected=" true "android:drawable=" @drawable/pic4 " /> <!--get focus when picture background--<item android:state_focused= "true" android:drawable= "@drawable/pic5"/> </ Selector>
Two. Using an XML file:
1. Method One: Configure the android:listselector= "@drawable/xxx in the ListView
or add an attribute android:background= "@drawable/xxx" in the ListView item
2. Method Two: Drawable drawable = Getresources (). getdrawable (R.DRAWABLE.XXX);
Listview.setselector (drawable); However, this will appear when the list is 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= "# "/></selector>
Button 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 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"/> </shape> </item></selector>
Finally, you need to add two items to the XML file that contains the button. For example Main.xml file, you need to add two android:focusable= "true" android:background= "@drawable/button_color" in <button/>
Android Background Selector Selector Usage Summary