Selector Property Description:
Android:state_selected selected
Android:state_focused Get Focus
android:state_pressed Click
Android:state_enabled setting whether to respond to an event, refers to all events
Use case Description:
1. Create a listview_selector.xml style file
First create a new drawable folder in the Res directory, and then in new drawable folder listview_selector.xml file , whose directory structure is: Res/drawable/< Span style= "padding:0px; margin:0px ">listview_selector.xml
2. Style edit
<?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"/> <!-- When you get the focus in non-touch mode and click the 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"/> <!--When selected picture background-- <item Android:state_selected= "true" android:drawable= "@drawable/pic4"/> <!--get focus when the picture background -- <item android:state_focused= "true" android:drawable= "@drawable/pic5"/> </selector>
3. Refer to listview_selector . xml file
There are three ways to refer to the file you just created:
(1) Add the following property code in the ListView
Android:listselector= "@drawable/listview_selector
(2) inAdd the following property code in the ListView item interface
android:background= "@drawable/listview_selector
(3) write directly with Java code
drawable drawable = Getresources (). getdrawable (R.drawable.listview_selector); Listview.setselector (drawable);
Attention:
If the ListView has a background picture or background color, the listview background will darken when you slide the listview. You can fix this problem by setting the following properties for the ListView
Android:cachecolorhint= "@android: Color/transparent"
When you click on a ListView item, you'll notice that the background color of the item turns orange. You can fix this problem by setting the following properties for the ListView
Android:listselector= "@android: Color/transparent"
All two of the above properties are set to the background color transparent, that is, #00000000 (transparent color)
The use of selector in Android---primarily to change the default background of the ListView and button controls