A Create XML file, location:drawable/xxx.xml, in the same directory remember to put related pictures
<?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 when!--no focus-- > <item android:state_window_focused= "false" android:drawable= "@drawable/pic1"/> <!--get focus in non-touch mode and click Background picture- <item android:state_focused= "true" android:state_pressed= "true" android:drawable= "@drawable/pic2"/> <!--touch mode when clicked background picture-- <item android:state_focused= "false" Android:state_pressed= "true" android:drawable= "@drawable/pic3"/> <!--selected picture background-- < Item android:state_selected= "True" android:drawable= "@drawable/pic4"/> <!--the picture background when getting focus-- <item android:state_focused= "true" android:drawable= "@drawable/pic5"/> </selector>
two . Use XML file:
1. Method One: Configure the android:listselector= "@drawable/xxx in the ListView
Or add properties to the ListView item android:background= "@drawable/xxx"
2. Method Two:drawable drawable = Getresources (). getdrawable (R.DRAWABLE.XXX);
Listview.setselector (drawable); but this will appear the list 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 Text effect in button:
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 gradients
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 pattern when the button is in the pressed state. <shape> <gradient android:startcolor= "#8600ff"/> <stroke android:w Idth= "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 shape when the button gets focus<shape> <gradient android:startcolor= "#eac100"/> <stroke and Roid: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 that contains the button . For example main.xml files that need to be in <button/> Riga two android:focusable= "true" android:background= "@drawable/button_color" The above just turn to see the project in the actual use of the selector, selector is really convenient!
There are two points to focus on:
1. Several different state attributes of view
2. How to change our background image according to different states.
In fact, the previously mentioned XML file will eventually be parsed into the Statelistdrawable class object by the Android framework .
Knowledge Point One: Statelistdrawable class Introduction
Class function Description: This class defines the corresponding image resources under different state values, that is, we can use this class to save a variety of state values, a variety of picture resources.
Common methods are:
public void addstate (int[] stateset, drawable drawable)
Function: Set drawable picture resource to a specific state collection
How to use: Refer to the previous hello_selection.xml file, we use the code to build an identical Statelistdrawable class object, as follows:
//Initialize an empty objectStatelistdrawable stalistdrawable =Newstatelistdrawable (); //gets the property value corresponding to the properties of the Android frame attrintpressed =Android. r.attr.state_pressed; intwindow_focused =Android. r.attr.state_window_focused; intfocused =Android. r.attr.state_focused; intSelected =Android. r.attr.state_selected; Stalistdrawable.addstate (New int[]{pressed, window_focused}, Getresources (). getdrawable (R.drawable.pic1)); Stalistdrawable.addstate (New int[]{pressed,-focused}, Getresources (). getdrawable (R.DRAWABLE.PIC2); Stalistdrawable.addstate (New int[]{selected}, Getresources (). getdrawable (R.DRAWABLE.PIC3); Stalistdrawable.addstate (New int[]{focused}, Getresources (). getdrawable (R.DRAWABLE.PIC4); //There is no state when the picture is displayed, we set it to my empty collectionStalistdrawable.addstate (New int[]{}, Getresources (). getdrawable (R.DRAWABLE.PIC5);
The "-" minus sign above indicates that the corresponding property value is False
When we use it as a background color for a view, the background image is converted based on the state.
public boolean isstateful ()
Function: Indicates that the state has changed, and whether the corresponding drawable image will change.
Note: In the statelistdrawable class, the method returns True, and when the state changes, our picture changes.
Knowledge Point two: Five status values for view
In general, the Android framework defines four different states for a view, which can cause view-related actions, such as changing the background image, whether
Trigger a Click event, etc;
View several different state meanings see:
Android Background Selector Selector Usage Summary