Android Selector usage

Source: Internet
Author: User

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.


 

 

 

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.