Android之Drawable-StateListDrawable

注意:本篇文章是本人阅读相关文章所写下的总结,方便以后查阅,所有内容非原创,侵权删。

本篇文章内容来自于:
1.Android开发艺术探索 任玉刚
2.领略千变万化的Android Drawable (一)

目录

  1. StateListDrawable
    --7.1 什么是StateListDrawable
    --7.2 StateListDrawable语法/属性
    --7.3 StateListDrawable使用案例

7. StateListDrawable

7.1 什么是StateListDrawable

StateListDrawable对于xml的<selector>标签,它表示Drawable集合,每个Drawable都对应着View的一种状态,这样系统就会根据view的状态来选择合适的Drawable。
StateListDrawable主要用于设置可单击的view的背景,常见为button。

7.2 StateListDrawable语法/属性

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    //android:constantSize StateListDrawable的固有大小是否随着其状态改变而改变,
    //因为在状态改变后,StateListDrawable会切换到别的Drawable,而不同的Drawable其大小可能不一样。
    //true表示大小不变,这时其固有大小是内容所有Drawable的固有大小的最大值。false则会随着状态改变而改变,默认值为false
    android:constantSize=["true" | "false"]
    //android:dither是否开启抖动效果,开启后可使高质量的图片在低质量的屏幕上仍然有较好的显示效果,一般建议开启,设置为true。
    android:dither=["true" | "false"]
    //android:variablePadding表示 StateListDrawable的padding是否随状态的改变而改变,默认值为false,一般建议设置为false就行。
    android:variablePadding=["true" | "false"] >
    <item
        //android:drawable 该状态下要显示的图像,可以是Drawable也可以是图片
        android:drawable="@[package:]drawable/drawable_resource"
        //android:state_pressed 表示是否处于被按下状态
        android:state_pressed=["true" | "false"]
        //android:state_focused 表示是否已得到焦点状态
        android:state_focused=["true" | "false"]
        //android:state_hovered 表示光标是否停留在View的自身大小范围内的状态
        android:state_hovered=["true" | "false"]
        //android:state_selected 表示是否处于被选中状态
        android:state_selected=["true" | "false"]
        //android:state_checkable 表示是否处于可勾选状态
        android:state_checkable=["true" | "false"]
        //android:state_checked 表示是否处于已勾选状态,一般用于CheckBox
        android:state_checked=["true" | "false"]
        //android:state_enabled 表示是否处于可用状态
        android:state_enabled=["true" | "false"]
        //android:state_active 表示是否处于激活状态
        android:state_activated=["true" | "false"]
        //android:state_window_focused 是否窗口已得到焦点状态
        android:state_window_focused=["true" | "false"] />
</selector>

7.3 StateListDrawable使用案例

效果:


第一步:定义xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_oval" android:state_focused="true"/>
    <item android:state_pressed="true">
        <shape  android:shape="rectangle">
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
</selector>

第二步:使用

    <Button
        android:id="@+id/btn_base64"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_button"
        android:text="Base64" />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容