A. Create XML file, location:drawable/xxx.xml, in the same directory remember to put related pictures
<?XML version= "1.0" encoding= "Utf-8"?> <selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--background picture by default - <Itemandroid:drawable= "@drawable/pic1" /> <!--background picture When there is no focus - <Itemandroid:state_window_focused= "false"android:drawable= "@drawable/pic1" /> <!--background picture When you get focus in non-touch mode and click - <Itemandroid:state_focused= "true"android:state_pressed= "true"android:drawable= "@drawable/pic2" /> <!--background picture when clicked in touch mode - <Itemandroid:state_focused= "false"android:state_pressed= "true"android:drawable= "@drawable/pic3" /> <!--picture Background When selected - <Itemandroid:state_selected= "true"android:drawable= "@drawable/pic4" /> <!--picture background when getting focus - <Itemandroid:state_focused= "true"android:drawable= "@drawable/pic5" /> </selector>
two . Use XML File:
1. Method One: in listview in configuration android:listselector = "@drawable/xxx
or in listview item Add Property android : background= "@drawable/xxx"
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.
Here is the configuration Button the text effect in:
Drawable/button_font.xml<?XML version= "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>button can also achieve more complex effects, such as gradient drawable/button_color.xml<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> / <Itemandroid:state_pressed= "true">//Defines the pattern when the button is in the pressed state. <Shape> <GradientAndroid:startcolor= "#8600ff" /> <StrokeAndroid:width= "2DP"Android:color= "#000000" /> <CornersAndroid:radius= "5DP" /> <paddingAndroid:left= "10DP"Android:top= "10DP"Android:bottom= "10DP"Android:right= "10DP"/> </Shape> </Item> <Itemandroid:state_focused= "true">//define the shape when the button gets focus<Shape> <GradientAndroid:startcolor= "#eac100"/> <StrokeAndroid:width= "2DP"Android:color= "#333333"Color= "#ffffff"/> <CornersAndroid:radius= "8DP" /> <paddingAndroid:left= "10DP"Android:top= "10DP"Android:bottom= "10DP"Android:right= "10DP"/> </Shape> </Item></selector>
Finally, you need to include button xml file added two items. For example main.xml files, required in < button/> riga two android : focusable= "true" android:background= "@drawable/button_color"