Selector in Android is mainly used to change the default background of ListView and Button controls. The usage can be designed as follows: (IN mylist_view.xml)
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:
[Html]
<SPAN style = "FONT-SIZE: 12px"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
</Selector> </SPAN>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
</Selector> you can define the style you want internally based on the project requirements. The main attributes are as follows:
[Html]
<SPAN style = "FONT-SIZE: 12px"> <? 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> </SPAN>
<? 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> 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:
[Html]
<SPAN style = "FONT-SIZE: 12px"> android: listSelector = "@ drawable/mylist_view" </SPAN>
Android: listSelector = "@ drawable/mylist_view" (2) Add the following property code to the ListView item Interface
[Html]
<SPAN style = "FONT-SIZE: 12px"> android: background = "@ drawable/mylist_view" </SPAN>
Android: background = "@ drawable/mylist_view" (3) write directly using JAVA code
[Html]
<SPAN style = "FONT-SIZE: 12px"> Drawable drawable = getResources (). getDrawable (R. drawable. mylist_view );
ListView. setSelector (drawable); </SPAN>
Drawable drawable = getResources (). getDrawable (R. drawable. mylist_view );
ListView. setSelector (drawable); to prevent list blacklisting, add the following attribute code to ListView:
[Html]
<SPAN style = "FONT-SIZE: 12px"> android: cacheColorHint = "@ android: color/transparent" </SPAN>
Android: cacheColorHint = "@ android: color/transparent" attributes:
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.