[Learn Android while working on projects] mobile security guard 08-layout and display details: State List, android08-

Source: Internet
Author: User

[Learn Android while working on projects] mobile security guard 08-layout and display details: State List, android08-

We noticed that the display status of buttons in some applications is different from that of normal ones when they are clicked, for example:

Normal: selected:

How can this effect be achieved? The Android system provides us with a convenient method to implement this function: state list drawable.

 

StateListDrawableIt is a drawable object defined in XML. We can set images under different items to display different States, depending on the status of the drawable object. For example, a Button control can have different States (click, focus, etc.), and provide different background images for different States through a series of drawable objects.

 

You can use an xml file to describe the status sequence.<selector>Element<item>Element to represent each background, where each<item>Different attribute values can be used to describe a status.

When the status changes, the status list is scanned from top to bottom, and the first image matching the current status is used. This selection is not based on the best match, but simply selects the first project that meets the conditions.

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_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. It is the root node and contains one or more nodes. -Android: constantSize: boolean type. The default value is false;

    • Android: dither: boolean type. The default value is true. When the bitmap is different from the pixel configuration of the screen (for example, a bitmap with an ARGB of 8888 and a screen with RGB of 555) dither ). If it is set to false, the color cannot be handed over;

    • Android: variablePadding: boolean type. The default value is false. When it is set to true, the padding value of drawable changes with the selected status.

  • <item>

    • Android: drawable: Required parameter, drawable resource;

    • Android: state_pressed: boolean type. If it is set to true, the item is displayed or effective when the object is pressed. If it is set to false, the item is not selected by default;

    • Android: state_focused: boolean type. If this parameter is set to true, the item takes effect when the focus is on the object, and false is not selected;

    • Android: state_selected: boolean type, same as above. This attribute indicates the selected status;

    • Android: state_checkable: boolean type. It is only used on widgets that can be selected. If it is set to true, it indicates that it is selectable. If it is set to false, it indicates that it is not optional;

    • Android: state_checked: boolean type. If the parameter is set to true, the item takes effect when it is selected. If the parameter is set to false, the item takes effect when it is not selected;

    • Android: state_enabled: boolean type. If this parameter is set to true, the item takes effect when the object is activated. For example, if the object accepts a touch or click event;

    • Android: state_window_focused: boolean, true indicates that the item takes effect when the current window focus is the application window, that is, the application window is foreground; otherwise, false;

1. Create your own. xml file in the/res/drawable directory, for example, button_selector.xml.

<?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"/>


At the same time, we can also set the effect when the GridView neutron control is clicked. I will not go into details here.

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.