in the development of the application, in many cases to design the ListView or button control background, the following summarizes the use of Android selector:
1. Configure the Android selector in drawable.
Save the following XML file as your own named. xml file (for example, Item_bg.xml) and place the file in the drawable file, using the corresponding background image based on the state of the list item in the ListView when the system is used.
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
<!--background picture by default--
<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 on the background picture--
<item android:state_focused= "true" android:state_pressed= "true"
android:drawable= "@drawable/pic2"/>
<!--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"/>
<!--get focus when the picture background--
<item android:state_focused= "true" android:drawable= "@drawable/pic5"/>
</selector>
2. Use the above configuration file:
The first method is configured in the ListView configuration file with the following code: android:listselector= "@drawable/item_bg"
The second method is to add a property to the ListView item, with the following code: android:background= "@drawable/item_bg"
The third method is set in Java code with the following code: drawable drawable =getresources (). getdrawable (R.DRAWABLE.ITEM_BG);
Listview.setselector (drawable);
the above setting method shows that the effect is sometimes black, so you need to add the following code to the configuration file: android:cachecolorhint= "@android: Color/transparent" makes the background transparent.
Similarly, the button has some background effects, which are explained in the following properties:
android:state_selected is the effect when the setting is selected
android:state_focused is setting the effect of getting focus
android:state_pressed is the effect of setting the click
android:state_enabled is setting whether to respond to events
The following is a selector used to set the state of text in a button
<?xmlversion= "1.0" encoding= "Utf-8"?>
<selectorxmlns:android= "Http://schemas.android.com/apk/res/android" >
<itemandroid:state_selected= "true" android:color= "#FFF"/>
<itemandroid:state_focused= "true" android:color= "#FFF"/>
<itemandroid:state_pressed= "true" android:color= "#FFF"/>
<itemandroid:color= "#000"/>
</selector>
Use of Android Selector