Usage of Selector in Android

Source: Internet
Author: User

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:

  1. <? Xmlversion = "1.0" encoding = "UTF-8"?>

  2. <Selectorxmlns: android = "http://schemas.android.com/apk/res/android">

  3. </Selector>


You can define the style you want internally according to the project requirements. The main attributes are as follows:

  1. <? Xmlversion = "1.0" encoding = "UTF-8"?>

  2. <Selectorxmlns: android = "http://schemas.android.com/apk/res/android">

  3. <! -- Default background image -->

  4. <Itemandroid: drawable = "@ drawable/pic1"/>

  5. <! -- Background image without focus -->

  6. <Itemandroid: state_window_focused = "false"

  7. Android: drawable = "@ drawable/pic1"/>

  8. <! -- Background image when the focus is obtained in non-touch mode and clicked -->

  9. <Itemandroid: state_focused = "true" android: state_pressed = "true" android: drawable = "@ drawable/pic2"/>

  10. <! -- Background image when clicked in touch mode -->

  11. <Itemandroid: state_focused = "false" android: state_pressed = "true" android: drawable = "@ drawable/pic3"/>

  12. <! -- Background of the selected image -->

  13. <Itemandroid: state_selected = "true" android: drawable = "@ drawable/pic4"/>

  14. <! -- Background of the image when the focus is obtained -->

  15. <Itemandroid: state_focused = "true" android: drawable = "@ drawable/pic5"/>

  16. </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:

  1. Android: listSelector = "@ drawable/mylist_view"


2) Add the following property code to the item interface of ListView:

  1. Android: background = "@ drawable/mylist_view"


3) use JAVA code to directly write

  1. Drawable drawable = getResources (). getDrawable (R. drawable. mylist_view );

  2. ListView. setSelector (drawable );


To prevent list blacklisting, add the following property code to ListView:

  1. 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

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.