Drawable(2)State list Drawable Resource介绍

State List


  A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state.

  You can describe the state list in an XML file. Each graphic is represented by an <item> element inside a single<selector> element. Each <item> uses various attributes to describe the state in which it should be used as the graphic for the drawable.

  During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.

FILE LOCATION:
  res/drawable/filename.xml
  The filename is used as the resource ID.
COMPILED RESOURCE DATATYPE:
  Resource pointer to a StateListDrawable.
RESOURCE REFERENCE:
  In Java: R.drawable.filename
  In XML: @[package:]drawable/filename
语法:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <selector xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:constantSize=["true" | "false"]
 4     android:dither=["true" | "false"]
 5     android:variablePadding=["true" | "false"] >
 6     <item
 7         android:drawable="@[package:]drawable/drawable_resource"
 8         android:state_pressed=["true" | "false"]
 9         android:state_focused=["true" | "false"]
10         android:state_hovered=["true" | "false"]
11         android:state_selected=["true" | "false"]
12         android:state_checkable=["true" | "false"]
13         android:state_checked=["true" | "false"]
14         android:state_enabled=["true" | "false"]
15         android:state_activated=["true" | "false"]
16         android:state_window_focused=["true" | "false"] />
17 </selector>
各元素介绍:
<selector>元素
  Required. This must be the root element. Contains one or more <item> elements.  
attributes:
xmlns:android StringRequired. Defines the XML namespace, which must be"http://schemas.android.com/apk/res/android".
android:constantSize

Boolean. "true" if the drawable's reported internal size remains constant as the state changes (the size is the maximum of all of the states);

"false" if the size varies based on the current state. Default is false.

android:dither

Boolean. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen

(for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to disable dithering. Default is true.

android:variablePadding

Boolean. "true" if the drawable's padding should change based on the current state that is selected; "false" if the padding should stay the

same (based on the maximum padding of all the states). Enabling this feature requires that you deal with performing layout when the state

changes, which is often not supported. Default is false.

<item>
  Defines a drawable to use during certain states, as described by its attributes. Must be a child of a<selector> element.

attributes:

android:drawable Drawable resourceRequired. Reference to a drawable resource.状态对应的资源图片.
android:state_pressed

Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked);

"false" if this item should be used in the default, non-pressed state.

android:state_focused

Boolean. "true" if this item should be used when the object has input focus (such as when the user selects a text input);

"false" if this item should be used in the default, non-focused state.(注意是input focus)

android:state_hovered

Boolean. "true" if this item should be used when the object is being hovered by a cursor; "false" if this item should be

used in the default, non-hovered state. Often, this drawable may be the same drawable used for the "focused" state.

Introduced in API level 14.当光标进入时,通常与focused相同.

android:state_selected

Boolean. "true" if this item should be used when the object is the current user selection when navigating with a directional

control (such as when navigating through a list with a d-pad); "false" if this item should be used when the object is not selected.

The selected state is used when focus (android:state_focused) is not sufficient (such as when list view has focus and

an item within it is selected with a d-pad).

android:state_checkable

Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is

not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)

android:state_checked Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
android:state_enabled

Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events);

"false" if it should be used when the object is disabled.

android:state_activated

Boolean. "true" if this item should be used when the object is activated as the persistent selection (such as to "highlight"

the previously selected list item in a persistent navigation view); "false" if it should be used when the object is not activated.

Introduced in API level 11.

android:state_window_focused

Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground),

"false" if this item should be used when the application window does not have focus (for example, if the notification

shade is pulled down or a dialog appears).应用窗口出现时.

Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).

EXAMPLE:
XML file saved at res/drawable/button.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 3     <item android:state_pressed="true"
 4           android:drawable="@drawable/button_pressed" /> <!-- pressed -->
 5     <item android:state_focused="true"
 6           android:drawable="@drawable/button_focused" /> <!-- focused -->
 7     <item android:state_hovered="true"
 8           android:drawable="@drawable/button_focused" /> <!-- hovered -->
 9     <item android:drawable="@drawable/button_normal" /> <!-- default -->
10 </selector>

This layout XML applies the state list drawable to a Button:

如何使用.

1 <Button
2     android:layout_height="wrap_content"
3     android:layout_width="wrap_content"
4     android:background="@drawable/button" />

 

 

posted @ 2015-09-23 23:58  f9q  阅读(211)  评论(0编辑  收藏  举报