selector、ColorStateList与StateListDrawable

参考:

Android ColorStateList 与StateListDrawable

android的ColorStateList简单使用

xml中selector:

定义:

txt_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/colorAccent" android:state_pressed="true" />
    <item android:drawable="@color/colorPrimaryDark" />

</selector>

txt_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="#45c01a" android:state_pressed="true" />
    <item android:color="#ff8041" />

</selector>

使用:

<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    android:text="Hello World!"
    android:background="@drawable/txt_bg"
    android:textColor="@drawable/txt_color"
    android:textSize="25sp"
    android:textStyle="bold" />

注意:要添加可点击效果,clickable、focusable

ColorStateList(常用于文字颜色):

类的关系:

public class ColorStateList extends ComplexColor implements Parcelable {

public abstract class ComplexColor {

示例:

<TextView
    android:id="@+id/tv2"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    android:text="Hello World!"
    android:textStyle="bold" />
// 颜色数组
int[] colors = new int[]{Color.BLUE, Color.RED};
// 颜色数组对应的状态
int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_pressed};
states[1] = new int[]{};//默认
ColorStateList colorList = new ColorStateList(states, colors);

//使用
TextView tv2 = (TextView) this.findViewById(R.id.tv2);
tv2.setTextColor(colorList);

StateListDrawable(用于背景颜色):

类的关系:

public class StateListDrawable extends DrawableContainer {

public class DrawableContainer extends Drawable implements Drawable.Callback {

public abstract class Drawable {

示例:

<TextView
    android:id="@+id/tv3"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    android:text="Hello World!"
    android:textSize="25sp"
    android:textStyle="bold" />
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(Color.BLUE));
stateListDrawable.addState(new int[]{}, new ColorDrawable(Color.RED));//默认

//使用
TextView tv3 = (TextView) this.findViewById(R.id.tv3);
tv3.setBackground(stateListDrawable);

ColorStateList与StateListDrawable共同点:

两者具有状态属性 ,状态定义在android.R.attr中:

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"]

选择方式:都是至上而下匹配(xml从上到下,java中int[] 从0~n),如果单个item中有两张状态声明,则同时满足两个状态才匹配成功

默认值:当所有条件都不满足的时候,会返回默认值。也就是最后一个值。

ColorStateList与StateListDrawable不同点:

ColorStateList 用于颜色选择

StateListDrawable 用于Drawable选择

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值