Android selector usage

Source: Internet
Author: User

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.


 

 

 

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.