Use of statelistdrawable and selector in Android

Source: Internet
Author: User

A different state is always indicated when the user is doing the action, such as when we press the button, the cursor moves to the button and the current state does not show a different state when the button is not pressed. In the Android system provides us with a convenient and to implement this function, namely: State list drawable.

Statelistdrawable is a drawable object defined in XML, we can display different states by setting a picture under different item, and its XML file is defined as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<selector xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:constantsize=["true" "false"]

Android:dither=["true" "false"]

Android:variablepadding=["true" "false"] >

<item

Android:drawable= "@[package:]drawable/drawable_resource"

Android:state_pressed=["true" "false"]

Android:state_focused=["true" "false"]

Android:state_selected=["true" "false"]

Android:state_active=["true" "false"]

Android:state_checkable=["true" "false"]

Android:state_checked=["true" "false"]

Android:state_enabled=["true" "false"]

Android:state_window_focused=["true" "false"]/>

</selector>

<selector> is the root node, and the next node has only one item

Android:constantSize:boolean type, default = False,

The Android:dither:boolean type, which defaults to true, when the bitmap is not the same as the screen's pixel configuration (for example, a bitmap with a ARGB of 8888 and an RGB 555 screen) will color itself (dither). When set to False, the non-recursive color.

Android:variablePadding:boolean, the default is False, and when set to true, the padding value of drawable changes with the state of the current selection.

<item> defines the picture resources that should be displayed when a certain state is selected by its properties;

Android:drawable: Required parameters, drawable resources;

The Android:state_pressed:boolean type, when set to True, indicates that item will appear or be in effect when the object is pressed, or false to indicate that item is the default state unchecked;

The Android:state_focused:boolean type, true when the item is active as the focus on the object, False is not selected;

Android:state_selected:boolean type, the same as the function, the attribute is selected when the state;

Android:state_checkable:boolean type, only used on the widget can be selected, True indicates optional, false means not selectable;

Android:state_checked:boolean, when True, indicates that the item is in effect when it is selected, and false is not checked when it takes effect;

Android:state_enabled:boolean, when True, the item takes effect when the object is active, such as when the object can accept touch or click events;

The Android:state_window_focused:boolean type, when True, indicates that the item takes effect when the current window focus is the application window, which means that the application window is foreground, otherwise false;

When we want to define a file of the state selection function that we need, it can be implemented mainly in two steps: Nutshell

1. Create your own. xml files, such as Select_button.xml, in the/res/drawable directory.

<?xml version= "1.0" encoding= "Utf-8"?>

<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >

<item android:state_pressed= "true"

android:drawable= "@drawable/button_pressed"/> <!--pressed--

<item android:state_focused= "true"

android:drawable= "@drawable/button_focused"/> <!--focused--

<item android:drawable= "@drawable/button_normal"/> <!--Default---

</selector>

2. Referencing in the layout XML file:

<imageview

android:layout_height= "Wrap_content"

Android:layout_width= "Wrap_content"

android:src= "@drawable/select_button"/>

PS Complete Example:

<selector

Xmlns:android= "Http://schemas.android.com/apk/res/android" >

<!--background color without focus--

<item android:state_window_focused= "false"

android:drawable= "@color/transparent"/>

<!--the background color when the focus is in non-touch mode and clicked-

<item android:state_focused= "true" android:state_pressed= "true"

android:drawable= "@color/bgred"/>

<!--the background color when clicked in touch mode--

<item android:state_focused= "false" android:state_pressed= "true"

android:drawable= "@color/bgred"/>

<!--background color when selected--

<item android:state_selected= "true" android:drawable= "@color/bgred"/>

<!--background color when getting focus--

<item android:state_focused= "true" android:drawable= "@color/bgred"/>

</selector>

Use of statelistdrawable and selector in Android

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.