Android Selector detailed

Source: Internet
Author: User

-Change the color of the font
<selectorXmlns:android="Http://schemas.android.com/apk/res/android"><!--the current window loses focus--<itemAndroid:color="@android: Color/black"Android:state_window_focused="False"/><!--unavailable--<itemAndroid:color="@android: Color/background_light"Android:state_enabled="False"/><!--compressions--<itemandroid:color= "@android: Color/holo_blue_light" android:state_ Pressed= "true" /> <!--selected--<item android:color= "@android: Color/holo_green_dark" android:state_ Selected= "true" /> <!--is activated--< Item android:color= "@android: Color/holo_green_light" android: State_activated= "true" /> <!--Default-- <item android:color= "@android: Color/white" /> </SELECTOR>

  --change the background of the button   
< Span class= "NT" > < Span class= "s" > < Span class= "NA" > &NBSP;            
? xml version= "1.0" encoding= "Utf-8"?><selectorXmlns:android="Http://schemas.android.com/apk/res/android"><!--the current window loses focus--<itemandroid:drawable="@drawable/bg_btn_lost_window_focused"Android:state_window_focused="False"/><!--unavailable--<itemandroid:drawable="@drawable/bg_btn_disable"Android:state_enabled="False"/><!--compressions--<itemandroid:drawable= "@drawable/bg_btn_pressed" android:state_ Pressed= "true" /> <!--selected--<item android:drawable= "@drawable/bg_btn_selected" android:state_ Selected= "true" /> <!--is activated--< Item android:drawable= "@drawable/bg_btn_activated" android: State_activated= "true" /> <!--Default-- <item android:drawable= "@drawable/bg_btn_normal" /></SELECTOR>            

So, in the process of use, there are a few points that need to be noted and understood:

    1. Selector as a drawable resource, the item specifies the android:drawable attribute and is placed in the drawable directory;
    2. Selector as a color resource, item specifies the Android:color attribute and is placed in the color directory;
    3. Color resources can also be placed in the drawable directory, referenced by @drawable to refer to, but it is not recommended to do so, drawable resources and color resources are best or separate;
    4. The android:drawable property can also reference @color color values in addition to referencing @drawable resources, but Android:color can only refer to @ Color;
    5. Item is matched from top to bottom, and if it matches to an item then it will take this item instead of the best match rule; so set the default state, be sure to write at the end, if written in front, then all the item will not work.

In addition, the selector tag has two more useful properties to say, after the addition of the following two properties, it will appear when the state changes fade effect, but must be at the API level 11 and above to support:

    • The fade-in time, in milliseconds, when the new state is displayed when the android:enterfadeduration state changes
    • The fade-out time, in milliseconds, when the old state disappears when the android:exitfadeduration state changes
< Span class= "NT" > < Span class= "s" > < Span class= "NA" > &NBSP;            
  --> ListView settings Selector   
< Span class= "NT" > < Span class= "s" > < Span class= "NA" >  
/span>
< Span class= "NT" > < Span class= "s" > < Span class= "NA" >
/span> < Span class= "NT" > < span class= "C" > finally , there are two ways to set the ListItem style of the ListView, one is to set the Android:listselector property in the ListView tab. The other is to set Android:background in ListItem layout layouts. However, the results of these two settings are different. At the same time, there are some other areas to note when using the ListView, which are summarized as follows

  1. android:listselector Set ListItem default background is transparent, no matter how you set in the selector can not change its background. Therefore, if you want to change the default background of ListItem, only in the second way, in the layout of listitem layouts android:backgroundset.
  2. When touch ListItem is clicked, the first setting,state_pressed,state_focused , and state_window_focused set to True will trigger , and the second setting means that only state_pressed will trigger.
  3. When a control such as a button or a checkbox is in the ListItem, the focus of the ListItem itself is preempted, causing the touch-click event of the ListItem itself to be invalid. So, to work around this problem, there are three solutions:

    • change a button or checkbox to a control such as TextView or ImageView
    • set a control setting such as a button or a checkbox Focusable property is False
    • set ListItem root layout properties android:descendantfocusability= Blocksdescendants "

    The third is the most convenient and recommended way to set all child controls under the ListItem root layout to not get the focus. There are three values for the android:descendantfocusability property, where ViewGroup refers to the view that sets the property, in this case the root layout of ListItem:

    • beforedescendants : ViewGroup takes precedence over its child control and gets to focus
    • afterdescendants : ViewGroup gets focus only if its subclass control does not need to get focus
    • blocksdescendants : ViewGroup overrides the subclass control and receives focus directly
< Span class= "NT" > < Span class= "s" > < Span class= "NA" > &NBSP;            

Android Selector detailed

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.