-
A Create XML file, location:drawable/xxx.xml, in the same directory remember to put related pictures
[HTML]View Plain Copy
- <? 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 when pressed 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"/ >
- <!--get focus when the picture background--
- <item android:drawable= "@drawable/handle_focused" android:state_focused="true" />
- <!--
- 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
- -->
- </selector>
two . Using an 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.
The following is the text effect in the configuration button:
Drawable/button_font.xml
[HTML]View Plain Copy
- <? 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
[HTML]View Plain Copy
- <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 pattern 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">//defines the shape 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 include add two items to the button's XML file. For example main.xml file, need to add two Android in <button/> :focusable= "true" android:background= "@drawable/ Button_color "This associates the Drawale/button_color.xml file with this control.
androida--Background Selector Selector Usage summary (i)