Properties and use of Android selector

Source: Internet
Author: User

All properties in 1.selector


2. Common attribute Analysis

Whether the android:state_accessibility_focused can get the focus
android:state_selected whether to select
Android:state_focused whether to get focus
android:state_pressed whether to click
Android:state_enabled setting whether to respond to an event, refers to all events
Android:state_checkable whether it is possible to select
Android:state_checked No is selected
Android:state_active is active
android:state_activated
Android:state_window_focused

3. Part of the use of code slices (others can only see your own needs to match)

<?xml version= "1.0" encoding= "Utf-8"?> <selector     xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <!--default background picture--      <item android:drawable=" @drawable/pic1 "/>      <!-- Background picture without focus--      <item android:state_window_focused= "false" android:drawable= "@drawable/pic1"/>     <!--get focus in non-touch mode and click 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 "/>    <!--selected picture background--      < Item android:state_selected= "True"   android:drawable= "@drawable/pic4"/>     <!--the picture background when getting focus--      <item android:state_focused= "true"   android:drawable= "@drawable/pic5"/></selector>  
This is used in the checkbox and RadioButton, the checkbox can be a toggle background, RadioButton can be multiple in radiogroup switch background
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ><!--selected background-    <item android:state_checked= "true" android:drawable= "@drawable/icon_ tabfriends_selected "></item><!--not selected on the background-    <item android:state_checked=" false "Android: drawable= "@drawable/icon_tabfriends_normal" ></item><!--default background, this can be--><item android:drawable= "@drawable/icon_tabfriends_normal" ></item></selector>

4. Three ways to use

(1) Adding attributes to the ListView

Android:listselector= "@drawable/xxx
(2) Add a property to the item in the ListView

android:background= "@drawable/xxx"
(3) in Java code

    drawable drawable = Getresources (). getdrawable (r.drawable.xxx);       Listview.setselector (drawable);  
To prevent the ListView from sliding to darken, you need to add

Android:cachecolorhint= "@android: Color/transparent"

There may be times when you have to use code to control, simple layout or not, such as ImageButton you have no way to implement RadioButton in the layout of the effect.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Properties and use of Android selector

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.