Android drawable resource learning (5) and statelistdrawable

Source: Internet
Author: User

A statelistdrawable is defined in an XML file. Based on the different states of the object, several different images are used to represent the same image. For example, a button has multiple states, such as getting focus, losing focus, and clicking. statelistdrawable can provide different backgrounds based on different states.

Describe these statuses in the XML file. Under the unique <selector> label, the <item> label is used to represent a graph. Each <item> tag uses various attributes to describe the drawable resources required for its status.

When the status changes again, the status list will be traversed from top to bottom, and the first matching will be used ------- instead of selecting the most suitable match.

File Location

res/drawable/filename.xmlThe filename is used as the resource ID.

Compile Data Type:

Pointer to statelistdrawable

Resource reference:

In Java: R.drawable.filenameIn XML: @[package:]drawable/filename

Syntax:

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

Element:

<Selector>: required. It must be the root element and contain one or more <item> elements.

Attribute

xmlns:android
String. Required.The namespace must be "http://schemas.android.com/apk/res/android".
android:constantSize
Boolean value. Whether the internal size (the largest among all States) is reported when it is changed. The default value is false.
android:dither
Boolean value. Whether to allow jitter if the bitmap and the screen pixel configuration are different (for example, the pixel setting of a bitmap is argb 8888, but the screen setting is RGB 565)
android:variablePadding
Boolean value. The default value is false. Whether to fill the drawing. (Not clear)
<item>
Defines a drawable for a State, which must be a child element of <selector>.

Attribute:

android:drawable
Mandatory, pointing to a drawable Resource
android:state_pressed
Boolean. Press or not
android:state_focused
Boolean. Get focus?
android:state_hovered
Boolean. Move the mouse over it. Normally, the same drawable as state_focused

Added after api14

android:state_selected
Boolean. Selected?
android:state_checkable
Boolean. Checkable ). It can only be used on the control that can be selected
android:state_checked
Boolean. Checked or not
android:state_enabled
Boolean. Available or not
android:state_activated
Boolean. Whether it is activated and persistent

Added after api11

android:state_window_focused
Boolean. Whether the current application gets the focus

Note: The Android system selects the first item that matches the current state .. Therefore, if the first item list contains all the State attributes, it only uses the State attributes each time. This is why your default value should be placed at the end.

Example:

Implementation of XML files:

<? XML version = "1.0" encoding = "UTF-8"?> <Selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <item Android: state_focused = "true" Android: drawable = "@ drawable/botton_add"/> <item Android: state_pressed = "true" Android: drawable = "@ drawable/botton_add_down"/> <item Android: state_selected = "true" Android: drawable = "@ drawable/botton_add"/> <item Android: drawable = "@ drawable/botton_add"/> // default </selector>
<Button         android:background="@drawable/statelist"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

Java code implementation:

Button BTN = (button) findviewbyid (R. id. BTN); statelistdrawable drawable = new statelistdrawable (); // if you want to set the mo item to false, add a negative sign in front, such as Android. r. ATTR. state_focesed flag is true,-android. r. ATTR. state_focesed indicates false drawable. addstate (New int [] {android. r. ATTR. state_focused}, this. getresources (). getdrawable (R. drawable. botton_add); drawable. addstate (New int [] {android. r. ATTR. state_pressed}, this. getresources (). getdrawable (R. drawable. botton_add_down); drawable. addstate (New int [] {android. r. ATTR. state_selected}, this. getresources (). getdrawable (R. drawable. botton_add); drawable. addstate (New int [] {}, this. getresources (). getdrawable (R. drawable. botton_add); // The default value is BTN. setbackgrounddrawable (drawable );
<Button        android:id="@+id/btn"         android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

Example:

Http://www.apkbus.com/forum.php? MoD = viewthread & tid = 52722.

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.