Android Application resource-drawable (3)

Source: Internet
Author: User

Status List

Statelistdrawable: A drawn object defined in XML. It depends on the object status and uses different images to represent the same image. For example, a button can have one of several different States (such as pressing, focusing, or none). By using the status list, you can provide different background images for each State.

The status list can be described in an XML file. Each graph is represented by a <item> element, which is placed in the <selector> element. Each <item> uses various attributes to describe the state to use when the image is drawn.

When the status changes, the status list will be traversed from top to bottom, and the first matching item will be used, and then the traversal ends. The selection is not based on the optimal matching rule.

File Location ):

Res/drawable/filename. xml

Filename is used for resource ID.

Data Type of the compiled resource (compiled resource datatype)

The resource points to a statelistdrawable object.

Resource reference ):

In Java code: R. drawable. filename

In XML: @ [Package:] drawable/filename

Syntax (syntax ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Selectorxmlns: 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 (elements ):

<Selector>

Required. It must be a root element. It must contain one or more <item> elements.

Attributes (attributes ):


Xmlns: Android


String Value, required. It defines the namespace for XML and must be a http://schemas.android.com/apk/res/android


Android: constantsize


A boolean value. If it is set to true, the drawing resource should report the internal size and use this size as the constant of state change (this size is the largest in all States ); otherwise, the size changes based on the current status. The default value is false.

Android: dither


A boolean value. If it is set to true, bitmap imitation processing is enabled. Otherwise, the imitation processing is disabled. The default value is true.


Android: variablepadding


A boolean value. If it is set to true, the margin of the drawn resource should be changed based on the selected status; otherwise, the margin should be the same (based on the maximum margin in all States ). To enable this function, you need to handle the layout when the status changes. Therefore, this setting is often not supported. The default value is false.

<Item>


Defines a resource used to draw a certain State. This resource is described by its attributes. It must be a child element of the <selector> element.

Attributes (attributes ):


Android: drawable


Required to draw resources. It points to the resource that can be referenced.


Android: state_pressed


A boolean value. If it is set to true, this item should be used when the object is pressed (for example, when the button is touched or clicked ). Otherwise, the project is used for the default non-pressed state.


Android: state_foucused


A boolean value. If it is set to true, this project should be used when the object has an input focus (for example, a text input field is selected); otherwise, this project application is used for the default non-focus state.


Android: state_houvered


A boolean value. If it is set to true, the project should be used when the cursor is hovering over the object. Otherwise, the default non-Hover state should be used. Generally, this drawing resource uses the same resource as the "Focus" state.


This status is introduced in API Level 14.


Android: state_selected


A boolean value. If it is set to true, this project should be used when you select a navigation control (for example, use a direction board to navigate to a list). If it is set to false, this state should not be used when the object is not selected.

When the focus status (Android: state_focused) does not meet your needs, you need to use the select status (for example, when the list view has a focus and you want to use the Guide Board to select a project in the list ).

Android: state_checkable


A boolean value. If it is set to true, this item should be used when the object can be checked. Otherwise, this object cannot be reproduced. (It is only useful when the object is transformed between optional and optional ).

Android: state_checked


A boolean value. If it is set to true, this project application is used when the object is checked; otherwise, it should be used in a non-Check state.

Android: state_enabled

A boolean value. If it is set to true, this item is applied to the object enabling status (the ability to receive touch or click events). Otherwise, it should be used to disable the status.

Android: state_activated

A boolean value. If it is set to true, this project should be used when the object is persistently selected (such as the highlighted state of the object). Otherwise, this State should be used when the object is not activated.

Android: state_window_focused

A boolean value. If it is set to true, this project should be used when the application window has focus (the application is in the foreground ). Otherwise, it should be used when the application window has no focus (for example, when the notification bar is displayed or the dialog box is displayed ).

Note: Remember that android uses the first item in the status list that matches the current status. Therefore, if the first item in the list does not contain any of the attributes described above, this project will be used at any time, which is why it is placed at the end of the list.

Column (example ):

The XML file is saved in RES/drawable/button. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Selectorxmlns: Android = "http://schemas.android.com/apk/res/android">
<Itemandroid: state_pressed = "true"
Android: drawable = "@ drawable/button_pressed"/> <! --
Pressed -->
<Itemandroid: state_focused = "true"
Android: drawable = "@ drawable/button_focused"/> <! --
Focused -->
<Itemandroid: state_hovered = "true"
Android: drawable = "@ drawable/button_focused"/> <! --
Hovered -->
<Itemandroid: drawable = "@ drawable/button_normal"/> <! --
Default -->
</Selector>

The following layout XML uses this status list on a button control:

<Button
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: Background = "@ drawable/button"/>

 

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.