A Create an XML file, location: Drawable/xxx.xml, under the same directory remember to put related pictures
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--background picture without focus-->
<item android:drawable= "@drawable/handle_normal" android:state_enabled= "true" android:state_window_focused= " False "/>
<!--touch Mode press the background picture-->
<item android:drawable= "@drawable/handle_pressed" android:state_pressed= "true"/>
<item android:drawable= "@drawable/handle_focused" android:state_enabled= "true" android:state_focused= "true"/ >
<item android:drawable= "@drawable/handle_normal" android:state_enabled= "true"/>
<!--picture background When the focus is obtained-->
<item android:drawable= "@drawable/handle_focused" android:state_focused= "true"/>
<!--
Android:state_selected is selected
Android:state_focused is getting the focus
Android:state_pressed is clicked
Android:state_enabled is setting whether to respond to events, which means all events
-->
</selector>
two . To use an XML file:
1. Method One:InListviewConfigured inAndroid:listselector= "@drawable/xxx
or inListviewOfItemAdd attribute inAndroid:background= "@drawable/xxx"
2. Method Two: drawable drawable = Getresources (). getdrawable (r.drawable. XXX);
Listview.setselector (drawable); But this can happen when the list is sometimes black, and you need to add:android:cachecolorhint= "@android: Color/transparent" make it transparent.
Related properties:
Android:state_selected is selected
Android:state_focused is getting the focus
Android:state_pressed is clicked
Android:state_enabled is setting whether to respond to events, which means all events
The selector effect of the button can also be set based on these states . You can also set selector to change The state of text in the button.
The following is the text effect in the Configure button:
Drawable/button_font.xml
Copy Code code as follows:
<?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>
A button can also achieve more complex effects, such as a gradient
Drawable/button_color.xml
Copy Code code as follows:
<span style= "COLOR: #330033" ><?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >/
<item android:state_pressed= "True" >//define the form 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 form 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></span><span style= "COLOR: #ff0000" > </SPAN>
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/> This associates the Drawale/button_color.xml file with the control.