Selector in Android is mainly used to change the default background of ListView and Button controls. You can follow the steps below to design the method:
Take mylist_view.xml as an example)
1. Create the mylist_view.xml File
First, create a drawable folder under the res directory, and then create mylist_view.xml in the new drawable folder. The directory structure is res/drawable/mylist_view.xml.
2. Edit the mylist_view.xml file as needed
After the mylist_view.xml file is created, its internal code structure is:
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<Selectorxmlns: android = "http://schemas.android.com/apk/res/android">
</Selector>
You can define the style you want internally according to the project requirements. The main attributes are as follows:
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<Selectorxmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Default background image -->
<Itemandroid: drawable = "@ drawable/pic1"/>
<! -- Background image without focus -->
<Itemandroid: state_window_focused = "false"
Android: drawable = "@ drawable/pic1"/>
<! -- Background image when the focus is obtained in non-touch mode and clicked -->
<Itemandroid: state_focused = "true" android: state_pressed = "true" android: drawable = "@ drawable/pic2"/>
<! -- Background image when clicked in touch mode -->
<Itemandroid: state_focused = "false" android: state_pressed = "true" android: drawable = "@ drawable/pic3"/>
<! -- Background of the selected image -->
<Itemandroid: state_selected = "true" android: drawable = "@ drawable/pic4"/>
<! -- Background of the image when the focus is obtained -->
<Itemandroid: state_focused = "true" android: drawable = "@ drawable/pic5"/>
</Selector>
3. Reference The mylist_view.xml File
Three methods can be used to reference the file just created:
1) Add the following property code to ListView:
Android: listSelector = "@ drawable/mylist_view"
2) Add the following property code to the item interface of ListView:
Android: background = "@ drawable/mylist_view"
3) use JAVA code to directly write
Drawable drawable = getResources (). getDrawable (R. drawable. mylist_view );
ListView. setSelector (drawable );
To prevent list blacklisting, add the following property code to ListView:
Android: cacheColorHint = "@ android: color/transparent"
Attribute introduction:
Android: state_selected
Android: state_focused get focus
Android: state_pressed click
Android: state_enabled: sets whether to respond to events. It indicates all events.
This article from "want to span, pay more" blog, please be sure to keep this source http://andywuchuanlong.blog.51cto.com/4898493/1297124