`
jguangyou
  • 浏览: 375668 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Drawable资源之StateListDrawable资源

 
阅读更多

    StateListDrawable用于组织多个Drawable对象。当使用StateListDrawable做为目标组件的背景、前景图片时,StateListDrawable对象显示的Drawable对象会随着目标组件的状态改变而改变。

 

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

 

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

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:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <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" />

 

 

最有有一点需要说明,<item> 的属性不但可以是android:drawable,还可以是android:color,当是color的时候程序也能正常运行,但是有一个更好的方法来定义多个颜色,那就是用ColorStateList。定义xml的时候和StateListDrawable一样,就是把android:drawable改成android:color。

 

 

分享到:
评论

相关推荐

    Drawable资源管理图片处理

    在Android开发中,Drawable资源是用于管理应用中各种图形元素,如图片、颜色、形状等的关键组成部分。了解和熟练掌握Drawable资源的管理与图片处理,对于优化应用性能和提升用户体验至关重要。以下是一些关于...

    Android中Drawable分类汇总

    Drawable资源是抽象的图形对象,可以包括图片、颜色、渐变等多种形式。在Android SDK中,开发者通常不需要直接操作Drawable的底层实现类,而是通过XML文件来定义和使用Drawable资源,通过资源ID来引用它们。 ...

    Android 中API之Drawable资源详解及简单实例

    StateListDrawable 是一种特殊的 drawable 资源,它可以根据不同的状态来显示不同的drawable。StateListDrawable 通常用来定义控件的背景、边框等,如按钮的 Enabled 状态、Disabled 状态,EditText 的获取焦点状态...

    android安卓APP之13种Drawable位图片资源.zip_android客户端是什么

    本资源包“android安卓APP之13种Drawable位图片资源.zip”涵盖了Android开发中常见的13种Drawable类型,这些类型极大地丰富了Android应用的视觉表现和动态效果。下面将详细介绍每种Drawable的特性和用法。 1. **...

    drawable(图片).zip

    本篇文章将深入探讨Android中的Drawable资源及其在压缩包"drawable(图片).zip"中的应用。 首先,Drawable是Android系统中用于表示图形对象的接口,它可以是位图(Bitmap)、形状(Shape)、动画(Animation)等。在...

    android drawable

    此外,`StateListDrawable`可以根据视图的状态(如按下、选中等)来显示不同的`Drawable`。 在实际开发中,`Drawable`经常与动画结合,例如`TransitionDrawable`实现颜色过渡效果,或者`Animatable Drawable`如`...

    博客《Android动画之一:Drawable Animation》附带源码 SwipeWithAnim

    这篇博客《Android动画之一:Drawable Animation》深入探讨了如何使用Drawable Animation来实现平滑的界面过渡效果,同时附带了源码供读者实践和学习。 Drawable Animation基于Drawable对象,它可以是一组连续的...

    Drawable.pdf

    ### 可绘制资源(Drawable资源)在Android中的运用与解析 在Android开发中,`Drawable`资源扮演着至关重要的角色,它们是图像的一种抽象表示,主要用于界面设计中的图标、背景和其他视觉元素。`Drawable`资源的灵活...

    android drawable下的xml详解

    **ColorDrawable**是最简单的`drawable`类型之一,它用于表示一个单一颜色的绘制区域。在XML布局文件中,可以通过`&lt;color&gt;`标签来定义一个颜色资源,并通过`android:color`属性指定具体的颜色值。例如,以下XML代码...

    Android Drawable 全部 xml 元素和属性用法

    - 使用Theme中的Drawable资源替代布局文件中的硬编码资源,以实现主题切换。 6. **自定义Drawable** 除了标准的Drawable类型,还可以通过继承`Drawable`类或`DrawableContainer`类(如`StateListDrawable`或`...

    drawable(图片).rar

    在Android项目的res/drawable目录下,开发者通常会根据需要将这些Drawable资源放入相应的子文件夹,如drawable-mdpi、drawable-hdpi等,以确保在不同密度的设备上正确显示。此外,Android Studio还支持vector ...

    Android Selecter Drawable属性使用源码下载

    2. `android:drawable`:在`&lt;item&gt;`标签内,设置与当前状态匹配时要显示的Drawable资源。 3. `android:state_pressed`、`android:state_focused`、`android:state_selected`、`android:state_checked`等:这些状态...

    安卓Drawable的使用

    1. **BitmapDrawable**:这是基于位图图像的Drawable,通常从本地文件、资源或网络加载。你可以通过`BitmapFactory.decodeResource()`方法或`BitmapDrawable(Bitmap)`构造函数创建BitmapDrawable。 2. **...

    DrawableStatesDemo:一个学习自定义Drawable以及学习DrawableState的demo

    4. **使用`StateListDrawable`**:`StateListDrawable`是一个预定义的Drawable,它可以包含多个子Drawable,每个子Drawable对应一种特定的组件状态。通过`addState()`方法添加状态及其对应的Drawable。 5. **测试和...

    Android中不同类型的Drawable使用

    BitmapDrawable是最基础的Drawable类型,用于展示位图资源,比如从本地文件、网络或资源ID加载的图片。可以通过设置BitmapDrawable的BitmapOptions来调整缩放、裁剪和过滤等参数。 2. **Shape Drawable** Shape ...

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

    首先,我们要理解Android中的Drawable资源。Drawable是Android图形库中的一个接口,用于表示屏幕上的图形对象,它可以是颜色、位图、形状等。在为View设置背景时,我们通常会用到Drawable。在这个案例中,我们只需要...

    SelectorDrawable2

    当视图处于这些状态之一时,对应的Drawable将被显示。 在实际应用中,我们通常将这个Selector设置为View的背景,例如按钮或TextView: ```java Button button = findViewById(R.id.my_button); button.set...

    android下菜单、颜色等资源文件的定义及访问

    Drawable资源是最常见的资源类型之一,主要用于存储图像、动画等视觉元素。这些资源通常存放在`res/drawable/`目录下,并可以通过以下方式访问: - **XML布局文件中访问**: ```xml android:id="@+id/...

    Android高级应用源码-drawable(图片).zip

    3. **StateListDrawable**:这种Drawable可以响应视图的不同状态,如按下、选中、默认等,并显示相应的图像。源码中可能会展示如何根据视图状态动态改变Drawable的外观。 4. **TransitionDrawable** 和 **...

Global site tag (gtag.js) - Google Analytics