`
Beyon_javaeye
  • 浏览: 67771 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

StateListDrawable

阅读更多
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

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_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>


elements:
<selector>
Required. This must be the root element. Contains one or more <item> elements.

attributes:
xmlns:android
String. Required. 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 resource. Required. 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 is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.

android:state_selected
Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.

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_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:


<?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:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

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

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />
分享到:
评论

相关推荐

    StateListDrawable例子

    在Android开发中,`StateListDrawable`是一种非常重要的图形对象,它允许我们根据视图的状态显示不同的图片或者颜色。在本篇文章中,我们将深入探讨`StateListDrawable`的属性及其用法,通过实例来理解其工作原理。...

    SelectorViewDrawable:使用StateListDrawable和ColorStateList以编程方式为Button,RadioButtons等创建背景和文本选择器

    SelectorViewDrawable用于在运行时为诸如Buttons,TextViews,RadioButtons等的视图创建选择器,而不是在drawable文件夹中添加多个xml资源。 有关教程,请访问我的博客

    应用源码之StateListDrawableSample.zip

    StateListDrawable是Android SDK中的一个关键组件,它在Android UI开发中扮演着重要角色,尤其在处理视图状态时。这个"应用源码之StateListDrawableSample.zip"压缩包文件显然是为了帮助开发者深入理解并实践如何...

    安卓Android源码——StateListDrawableSample.rar

    StateListDrawable是Android SDK中的一个关键图形资源类型,它允许开发者根据组件的状态(如按下、聚焦、默认等)显示不同的图像。在Android应用开发中,StateListDrawable常用于按钮、背景和其他视图元素,以实现...

    Android应用源码之StateListDrawableSample.zip

    StateListDrawable是Android SDK中的一个关键组件,它在Android图形和UI设计中起着至关重要的作用。这个"Android应用源码之StateListDrawableSample.zip"压缩包提供的源码示例,旨在帮助开发者深入理解如何使用...

    小程序源码 StateListDrawableSample.zip

    StateListDrawable是Android SDK中的一个资源类型,它允许我们在不同的状态下显示不同的图像。这个"小程序源码 StateListDrawableSample.zip"很可能包含了一个小程序或应用的源代码示例,专门用于演示如何使用...

    使用单张图片为View设置带点击效果的背景 - Java - 下载.zip

    1. 创建一个StateListDrawable对象:`StateListDrawable states = new StateListDrawable();` 2. 定义不同状态的Drawable:例如,我们可以为默认状态和按下状态设置不同的图片。 - 对于默认状态(未被点击):`...

    Android应用源码之StateListDrawableSample-IT计算机-毕业设计.zip

    StateListDrawable是Android SDK中的一个关键组件,常用于Android应用的UI开发,特别是在处理不同状态下的图形绘制时。这个"Android应用源码之StateListDrawableSample"是一个示例项目,旨在帮助开发者理解如何使用...

    Android安卓经典设计学习例程源代码-StateListDrawableSample.rar

    StateListDrawable是Android系统中一个重要的图形资源类型,它允许我们根据组件的状态(如按下、聚焦、默认等)来显示不同的图像。这个"StateListDrawableSample"示例代码旨在帮助开发者深入理解如何在应用程序中...

    高亮显示正在输入的文本框

    这种效果通常通过自定义`StateListDrawable`实现,允许开发者根据不同的组件状态显示不同的图形。 `StateListDrawable`是Android系统提供的一个Drawable类,它可以绑定到View的状态上,根据View的不同状态(如按下...

    Android State List Drawable状态列表绘制实例.rar

    在Android开发中,State List Drawable(状态列表绘制对象)是一种非常重要的资源类型,它允许我们根据组件的状态(如被按下、被聚焦、被选中等)来改变其显示的图像。这个实例“Android State List Drawable状态...

    基础安卓自学汇总(网课学习)

    * StateListDrawable:根据不同的状态,设置不同的图片效果 * Button 事件处理: + 点击事件 + 长按事件 + 触摸事件 3. EditText 基础知识点 * 主要属性: + android:hint + android:textColorHint + ...

    Android 2.2 r1 API 中文文档系列(12) —— Button

    除了使用`StateListDrawable`来自定义背景外,还可以设置其他属性来进一步定制按钮,如`android:textColor`、`android:textSize`等。 #### 总结 `Button`组件在Android开发中扮演着极其重要的角色。通过合理的...

    安卓Android源码——不用更换图片的点击效果.zip

    如果需要在代码中动态设置按钮的点击效果,可以使用`setBackgroundResource()`或`setBackgroundDrawable()`方法,传入我们自定义的StateListDrawable资源。 7. **优化性能**: 尽管自定义Drawable可以提供丰富的...

    安卓Android源码——按钮点击WIN8 磁贴效果 (2).zip

    在安卓Android开发中,创建独特的用户交互体验是提升应用程序吸引力的关键...通过自定义View、使用动画和StateListDrawable,开发者可以创建出吸引用户的独特交互体验。实践这些技巧,你的Android应用将更加生动有趣。

    Android ListView item 选中高亮显示

    如果需要更复杂的高亮效果,比如自定义背景色或者动画,可以使用StateListDrawable。StateListDrawable可以根据View的状态(如是否被选中)显示不同的图像资源。在XML中定义一个StateListDrawable,然后将其设置为...

    DrawableDemo.zip

    - 实现StateListDrawable,为不同状态(如pressed、focused)定义不同Drawable,通过`&lt;item&gt;`标签定义状态和对应的Drawable。 - 利用LevelListDrawable,通过level值的变化来切换Drawable,比如进度条的实现。 - ...

    Android代码-Android SVG library

    and can effectively be used wherever a conventional image would be displayed, whether it be as a background, ImageView source, inside a StateListDrawable or used as composites in a TextView. ...

    玩转Android之Drawable的使用

    - StateListDrawable:根据视图状态(如按下、选中等)显示不同Drawable,常用于按钮和列表项的背景。 - LevelListDrawable:根据特定级别显示不同的Drawable,常用于进度条和评分系统。 - TransitionDrawable:用于...

    Android图像介绍-Drawable

    5. StateListDrawable:状态列表Drawable,可以根据控件的状态(如按下、选中、默认)显示不同的图像。这对于按钮、选择器等需要根据状态改变外观的组件非常有用。 6. LevelListDrawable:层级列表Drawable,根据一...

Global site tag (gtag.js) - Google Analytics