The use of selector in Android---primarily to change the default background of the ListView and button controls

Source: Internet
Author: User
Tags transparent color

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.