State List

转载 2015年11月18日 10:07:55

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 neither) 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.

<?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>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

应用

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

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

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).


android:drawable 放一个drawable资源
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活(这个麻烦举个例子,不是特明白)
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

 

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。


官方的API http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

上边就是我从官网复制过来的……





android-Color State List Resource

> A ColorStateList is an object you can define in XML that you can apply as a color, but will actu...
  • ShareUs
  • ShareUs
  • 2015年12月03日 14:35
  • 179

Drawable之State list

Drawable之State list
  • fumier
  • fumier
  • 2015年06月22日 15:00
  • 418

Android学习笔记--state list

            在用户进行操作的时候总是会提示出不同的状态,比如当我们按下button时,光标移动到button上并没有按下以及当前状态不在该button时总会显示不同的状态,在Android...
  • phxuecan
  • phxuecan
  • 2011年03月16日 15:30
  • 6907

Android录制视频报错setVideoSize called in a invalid state 1

出现了两次错误,一次是录制视频时调用camera.getParameters()时报`parameters is empty`,还有一个错误就是`setVideoSize called in a in...
  • u010387196
  • u010387196
  • 2015年04月28日 21:58
  • 1536

Color State List Resource

A ColorStateList is an object you can define in XML that you can apply as a color, but will actually...
  • jthivan
  • jthivan
  • 2016年02月23日 16:19
  • 92

android开发 color state list

一个ColorStateList是一个对象,定义在xml中,用它作为颜色值来使用。但是否显示出颜色取决于视图对象的状态。例如,一个按钮有几个不同的状态(按下,获得焦点)    ,可以使用colo...
  • ltl451011
  • ltl451011
  • 2011年11月08日 15:57
  • 1227

Android State List

State List A StateListDrawable is a drawable object defined in XML that uses a several different im...
  • chenjianjk
  • chenjianjk
  • 2013年09月14日 10:00
  • 468

[React网络整理]React之State的学习笔记

HTML 标签对比 React 组件React 可以渲染 HTML 标签 (strings) 或 React 组件 (classes)。 要渲染 HTML 标签,只需在 JSX 里使用小写字母开头的...
  • itpinpai
  • itpinpai
  • 2016年12月16日 11:33
  • 1626

AnimatedStateListDrawable

。。。。。。。。。。
  • L25000
  • L25000
  • 2015年11月12日 16:38
  • 601

11、React系列之--state状态

版权声明:本文为博主原创文章,未经博主允许不得转载。 PS:转载请注明出处 作者:TigerChain 地址:http://www.jianshu.com/p/44a787904d9b 本文出自...
  • android0012345
  • android0012345
  • 2017年04月28日 09:11
  • 801
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:State List
举报原因:
原因补充:

(最多只允许输入30个字)