Android selector background selector
Both listview and button need to change the background of the original android control, and some information on the Internet is not very comprehensive, so let's summarize the usage of android selector.
First, the android selector is configured in drawable/xxx. xml.
First look at the status in listview:
Save the following XML file as your own. xml file (for example, list_item_bg.xml). When using the system, you can use the corresponding background image based on the status of the list items in the ListView.
Drawable/list_item_bg.xml
<?xml version= "1.0" encoding= "utf-8" ?> <selector xmlns:android= "http://schemas.android.com/apk/res/android" > <! -- Default background image --> <item android:drawable= "@drawable/pic1" /> <! -- Background image without focus --> <item android:state_window_focused= "false" android:drawable= "@drawable/pic1" /> <! -- Background image when the focus is obtained in non-touch mode and clicked --> <item android:state_focused= "true" android:state_pressed= "true" android:drawable= "@drawable/pic2" /> <! -- Background image when clicked in touch mode --> <item android:state_focused= "false" android:state_pressed= "true" android:drawable= "@drawable/pic3" /> <! -- Background of the selected image --> <item android:state_selected= "true" android:drawable= "@drawable/pic4" /> <! -- Background of the image when the focus is obtained --> <item android:state_focused= "true" android:drawable= "@drawable/pic5" /> </selector> |
Use some xml files: first, configure android: listSelector = "@ drawable/list_item_bg" in listview"
You can also add the property android: background = "@ drawable/list_item_bg" to the listview item, or use Drawable drawable = getResources () in java code (). getDrawable (R. drawable. list_item_bg );
ListView. setSelector (drawable); same effect.
However, sometimes the list is black. You need to add android: cacheColorHint = "@ android: color/transparent"
Make it transparent.
Next, let's take a look at some background Effects of the Button:
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 as follows:
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="#000" />
</selector>
Button can also achieve more complex effects, such as gradient.
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 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" > <! -- Define 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> |
Finally, you need to add two items to the xml file containing the button. For the main. xml file, we need to add two items in <Button/>.
Android: focusable = "true"
Android: backgroud = "@ drawable/button_color"
In this way, when you use the Button, you can get rid of the yellow background that comes with the system. It is very useful to achieve a personalized background and cooperate with the overall layout of the application.