This post is reproduced, not prepared by myself.
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. CreateMylist_view.xml File
Create a drawable folder in the res directory, and thenCreate in the new drawable folderMylist_view.xml, whose directory structure is res/Drawable/Mylist_view.xml.
2. Edit as neededMylist_view.xml File
NewAfter the mylist_view.xml file is addedCodeStructure:
<? 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 according to the project requirements. The main attributes are as follows:
3. ReferenceMylist_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) InAdd the following property code to the listview item interface:
Android: Background = "@ drawable/mylist_view"
(3) using Java code for Direct Writing
Drawable = getresources (). getdrawable (R. drawable. mylist_view); listview. setselector (drawable );
To prevent list blacklisting, you mustAdd the following to listview:AttributesCode
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.