1. Create an xml file at drawable/xxx. xml. Remember to put the relevant images in the same directory.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Background image without focus -->
<Item android: drawable = "@ drawable/handle_normal" android: state_enabled = "true" android: state_window_focused = "false"/>
<! -- Background image in touch mode -->
<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"/>
<! -- Background of the image when the focus is obtained -->
<Item android: drawable = "@ drawable/handle_focused" android: state_focused = "true"/>
<! --
Android: state_selected is selected
Android: state_focused is the focal point
Android: state_pressed: Click
Android: state_enabled is used to set whether to respond to events. It refers to all events.
-->
</Selector>
Ii. Use an xml file:
1. Method 1: Configure android: listSelector = "@ drawable/xxx in listview
Alternatively, you can add the property android: background = "@ drawable/xxx" to the listview item"
2. Method 2: Drawable drawable = getResources (). getDrawable (R. drawable. xxx );
ListView. setSelector (drawable); however, when the list is sometimes black, you must add android: cacheColorHint = "@ android: color/transparent" to make it transparent.
Related attributes:
Android: state_selected is selected
Android: state_focused is the focal point
Android: state_pressed: Click
Android: state_enabled is used to set whether to respond to events. It refers to all events.
You can also set the selector effect of the button Based on these statuses. You can also set selector to change the text status in the button.
Configure the text effect in the button:
Drawable/button_font.xml
[Html]
<? 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>
Button can also achieve more complex effects, such as gradient
Drawable/button_color.xml
[Html]
<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"> // defines the form of a button when it 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"> // defines the form when the button obtains the 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 containing the button. For example, main. xml file, you need to add two android: focusable = "true" android: background = "@ drawable/button_color" to associate the drawale/button_color.xml file with this control.