"Side do project side learn Android" mobile Security defender 08-some layout and display details: State List

Source: Internet
Author: User

We have noticed that the buttons in some applications are not the same as the normal state when they are clicked, such as:

In normal state: In the selected state:

How is this effect achieved? 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, depending on the state of the Drawable object. For example, a button control can have different states (click, Focus, and so on), and with a series of drawable objects, you can provide a different background image for each different state.

This sequence of states can be described by an XML file that <selector> <item> represents each background with elements under one element, each of which <item> can describe a state using different attribute values.

When the state changes, the status list is scanned from top to bottom, and the first picture that matches the current state is used. This option is not based on best bets, but simply selects the first item that meets the criteria.

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=["t Rue "| "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"]/></selector> 


  • <selector>

    Required. is the root node that contains one or more nodes. -Android:constantSize:boolean type, default is False,

    • Type Android:dither:boolean, default is true, when the bitmap is different from the screen's pixel configuration (for example, A ARGB 8888 bitmap and RGB 555 screen) will be self-recursive (dither). The non-recursive color when set to False,

    • Type Android:variablePadding:boolean, false by default, and when set to True The padding value of drawable changes with the state of the current selection.

  • <item>

    • 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;

1. Create your own. xml files, such as Button_selector.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_enabled=" true "android:state_window_focused=" false "android:drawable=" @ Drawable/button_background "/>    <item android:state_pressed=" true "android:state_window_focused=" true " android:drawable= "@drawable/button_background_selected"/>    <item android:state_focused= "true"  android:drawable= "@drawable/button_background"/>    <item android:drawable= "@drawable/button_background" /></selector>


2. Reference in the layout XML file:

<button            android:id= "@+id/bt_first_dialog_confirm"            android:layout_width= "140dip"            android:layout_ height= "40dip"            android:background= "@drawable/button_selector"            android:text= "OK"            android:textcolor= "#ffffffff"/>


We can also set the effect of the GridView neutron control when clicked. We will not repeat it here.

"Side do project side learn Android" mobile Security defender 08-some layout and display details: State List

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.