Android入门——选择器selelctor之ColorStateList和StateListDrawable使用小结

引言

相信很多开发者说到Android的选择器,第一时间相到应该就是selector了吧,而应用的最多的应该就是用于同步切换View的背景了吧,往往有些忽略了selector不仅仅可以用于切换背景也可以动态改变View的颜色,当然这并不是什么很复杂的知识点。不过我想总结下这篇文章并不是因为这个原因,今天在自定义一个View的时候,获取对应的颜色值并设置到TextView上,一个setTextColor(int )让我很迷惑,(有些颜色只用五位整数来表达,而有的颜色却用了八位而且还都带一个负号“-”,不是我们普通认知的RGB或者其他模式),追踪源码发现了ColorStateList对象…

一、StateListDrawable

1、StateListDrawable概述

StateListDrawable作为Drawable系中重要的一员家族成员,该类定义了不同状态值下与之对应的图片资源,即我们可以利用该类保存多种状态android:state_pressedandroid:state_checkableandroid:state_selectedandroid:state_focusedandroid:state_enabledandroid:state_window_focusedandroid:state_checkedandroid:state_firstandroid:state_activeandroid:state_lastandroid:state_middle 值,多种图片资源。

2、StateListDrawable基本语法

2.1、StateListDrawable xml文件路径

StateListDrawable也是一个图片资源,也会和其他字符串一样再R文件中自动生成。

res/drawable/filename.xml

2.2、使用StateListDrawablexml

  • 在Java代码中使用——R.drawable.filename
  • 通过xml静态引用——@[package:]drawable/filename

2.3、静态xml文件的结构

  • 根节点——必须是selector
  • 命名空间——xmlns:android=”http://schemas.android.com/apk/res/android”
  • item节点里的drawable元素节点用于设置具体的图片drawable

3、StateListDrawable主要的成员方法

方法名说明
public StateListDrawable()构造方法
void addState(int[] stateSet, Drawable drawable)给特定的状态集合设置drawable资源,其中“-”负号表示对应的属性值为false
void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
从XML资源中加载对应的Drawable
boolean isStateful()当状态改变的时候是否同步改变对应的Drawable,true则同步改变

4、StateListDrawable所支持的11种状态

状态属性值含义
android:state_active是否处于激活状态
android:state_checkable是否可勾选
android:state_checked是否已勾选
android:state_enabled是否可用
android:state_first是否开始状态
android:state_focused是否已得到焦点
android:state_last是否处于结束
android:state_middle是否处于中间
android:state_pressed是否处于按下状态 .
android:state_selected是否处于选中状态
android:state_window_focused是否窗口已获得焦点
<?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>

5、StateListDrawable的使用

5.1、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>
<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

5.2、Java动态代码构造并使用

//初始化一个空对象
StateListDrawable stalistDrawable = new StateListDrawable();
//获取对应的属性值 Android框架自带的属性 attr
int pressed = android.R.attr.state_pressed;
int window_focused = android.R.attr.state_window_focused;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;
stalistDrawable.addState(newint []{pressed , window_focused}, getResources().getDrawable(R.drawable.pic1));
stalistDrawable.addState(newint []{pressed , -focused}, getResources().getDrawable(R.drawable.pic2);
stalistDrawable.addState(newint []{selected }, getResources().getDrawable(R.drawable.pic3);
stalistDrawable.addState(newint []{focused }, getResources().getDrawable(R.drawable.pic4);
//没有任何状态时显示的图片,我们给它设置我空集合
stalistDrawable.addState(newint []{}, getResources().getDrawable(R.drawable.pic5);

二、ColorStateList

1、ColorStateList概述

ColorStateList直接继承Object通过实现Parcelable接口达到序列化,虽然不是Drawable系的子类,但是其实也可以看成是一个Drawable对象。从字面命名理解就可以知道这是一个与状态相关的,是一个可以在资源文件里/res/color中定义的对象,可以根据View对象的android:state_pressedandroid:state_checkableandroid:state_selectedandroid:state_focusedandroid:state_enabledandroid:state_window_focusedandroid:state_checked状态变化同步切换对应的颜色。 比如,Button可以存在于几种不同状态之一(按下,聚焦或更靠近),通过使用颜色状态列表ColorStateList,我们可以在各个状态期间同步切换不同的颜色。简而言之,它是我们的一种selector。

2、ColorStateList的基本语法

2.1、ColorStateList xml文件路径

ColorStateList也是一个资源,也会和其他字符串、图片资源一样再R文件中自动生成。

res/color/filename.xml

2.2、使用ColorStateList

  • 在Java代码中使用——R.color.filename
  • 通过xml静态引用——@[package:]color/filename

2.3、静态xml文件的结构

  • 根节点——必须是selector
  • 命名空间——xmlns:android=”http://schemas.android.com/apk/res/android”
  • item节点里的color元素节点用于设置具体的颜色值,支持多种颜色模式

android:color

#RGB
#ARGB
#RRGGBB
#AARRGGBB
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:color="@color/testcolor1"/>
   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
   <item android:state_enabled="false" android:color="@color/testcolor3" />
   <item android:color="@color/testcolor5"/>
 </selector>

2.4、ColorStateList 所支持的7种状态说明

  • android:color或者android:drawable:可设置指定颜色或Drawable对象
    • adnroid:state_xxx:一个特定的状态
状态属性值含义
android:state_checkable是否可勾选
android:state_checked是否已勾选
android:state_enabled是否可用
android:state_focused是否已得到焦点
android:state_pressed是否处于按下状态 .
android:state_selected是否处于选中状态
android:state_window_focused是否窗口已获得焦点

2.5、ColorStateList的主要成员方法

方法名说明
public ColorStateList(int[][] states, int[] colors)构造方法
static ColorStateList createFromXml(Resources r, XmlPullParser parser)通过xml资源文件建立对应的ColorStateList对象
int getColorForState(int[] stateSet, int defaultColor)
获取状态对应的颜色
int getDefaultColor()
static ColorStateList valueOf(int color)把代表颜色的整数转为ColorStateList

3、ColorStateList的使用

res/color/button_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

3.1、xml中静态使用ColorStateList

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />

3.2、Java代码动态使用ColorStateList

3.2.1、第一种方式
Button btn=(Button)findViewById(R.id.btn); 
Resources resource=(Resources)getBaseContext().getResources();  
 ColorStateList colors=(ColorStateList)resource.getColorStateList(R.color.btn_text); 
 if(colors!=null){  
    btn.setTextColor(color_state_list);//设置按钮文字颜色 
 }  
3.2.2、第二种方式
XmlResourceParser xpp=Resources.getSystem().getXml(R.color.button_text); 
try {
     ColorStateList colors= ColorStateList.createFromXml(getResources(),xpp);
     btn.setTextColor(colors);
} catch (Exception e) {

}

PS: TextView的setTextColor(int )的一些注意事项

  1. tv.setTextColor(android.graphics.Color.RED);//系统自带的颜色类
  2. tv.setTextColor(0xffff00ff);//0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示。
  3. tv.setTextColor(this.getResources().getColor(R.color.red));//通过获得资源文件进行设置。根据不同的情况R.color.red也可以是R.string.red或者R.drawable.red,当然前提是需要在相应的配置文件里做相应的配置,如:
/**
*千万不能把自己定义的颜色直接当成参数,例如:
tv.setTextColor(R.color.red);这种情况会出现颜色错误!
**/
<color name="red">#FF0000</color>

< drawable name="red">#FF0000</drawable>

< string name="red">#FF0000</string>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CrazyMo_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值