`
jguangyou
  • 浏览: 369034 次
  • 性别: 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。

 

 

分享到:
评论

相关推荐

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

    Android 中API之Drawable资源 1、最常用的StateListDrawable  说StateListDrawable,很多Android猿可能感到不太熟悉,不过如果说selector选择器,肯定都会恍然大悟,不错,这两个东西就是同一个~~ 它的用途之广,...

    StateListDrawable例子

    Drawable系列(四)——StateListDrawable的属性和使用介绍一文Demo下载资源

    疯狂Android讲义(第2版)源代码 第6章~第9章

    6.4、使用Drawable资源:图片资源; StateListDrawable资源; LayerDrawable资源; ShapeDrawable资源; ClipDrawable资源; AnimationDrawable资源; 6.5、使用原始XML资源: 6.6、使用Layout资源: 6.7、使用菜单...

    Android实例代码

    6.4、使用Drawable资源:图片资源; StateListDrawable资源; LayerDrawable资源; ShapeDrawable资源; ClipDrawable资源; AnimationDrawable资源; 6.5、使用原始XML资源: 6.6、使用Layout资源: 6.7、使用菜单...

    疯狂Android讲义源码

     6.4.2 StateListDrawable资源 225  6.4.3 LayerDrawable资源 227  6.4.4 ShapeDrawable资源 229  6.4.5 ClipDrawable资源 231  6.4.6 AnimationDrawable资源 233  6.5 使用原始XML资源 236  6.5.1 定义原始...

    疯狂Android讲义.part2

    6.4.2 StateListDrawable资源 225 6.4.3 LayerDrawable资源 227 6.4.4 ShapeDrawable资源 229 6.4.5 ClipDrawable资源 231 6.4.6 AnimationDrawable资源 233 6.5 使用原始XML资源 236 6.5.1 定义原始XML资源 236 ...

    疯狂Android讲义.part1

    6.4.2 StateListDrawable资源 225 6.4.3 LayerDrawable资源 227 6.4.4 ShapeDrawable资源 229 6.4.5 ClipDrawable资源 231 6.4.6 AnimationDrawable资源 233 6.5 使用原始XML资源 236 6.5.1 定义原始XML资源 236 ...

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

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

    APP开发教程 Java Android移动端开发 4、Android UI进阶编程 (1) 共34页.pptx

    Android Drawable 概述 Bitmap & .9.png Shape XML StateListDrawable Android图形编程 类简介 Canvas Paint Typeface

    Android学习系列教程实例.pdf

    DevDiv 推荐资源 ........................ 2 Windows 8 ........................................................... 2 iOS ....................................................................... 2 Android...

Global site tag (gtag.js) - Google Analytics