Android控件 状态颜色控制ColorStateList简单使用

本文详细介绍了如何在Android中通过XML和Java创建ColorStateList,包括基本用法示例、扩展的ColorStateListBuilder类,并罗列了所有状态类型的参考。适合开发者掌握颜色状态选择器的动态设置技巧。

Refer to : How do I create ColorStateList programmatically?
https://stackoverflow.com/questions/15543186/how-do-i-create-colorstatelist-programmatically?answertab=active#tab-top

一、基本使用

xml 代码与 java 代码对比

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white"/>
    <item android:color="@color/black"/>
</selector>
ColorStateList myColorStateList = new ColorStateList(
    new int[][]{
            new int[]{android.R.attr.state_pressed},
            new int[]{}//default
    },
    new int[] {
            context.getResources().getColor(R.color.white),
            context.getResources().getColor(R.color.black)
    }
);

使用示例 1:

int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_selected};//selected state
states[1] = new int[]{};//default state

int[] colors = new int[]{
        Color.parseColor(bottomBar.activeColor), //selected state color
        Color.parseColor(bottomBar.inActiveColor)//default state color
};

ColorStateList colorStateList = new ColorStateList(states, colors);

使用示例 2:

int[][] states = new int[][] {
    new int[] { android.R.attr.state_enabled}, // enabled
    new int[] {-android.R.attr.state_enabled}, // disabled
    new int[] {-android.R.attr.state_checked}, // unchecked
    new int[] { android.R.attr.state_pressed}  // pressed
};

int[] colors = new int[] {
    Color.BLACK,
    Color.RED,
    Color.GREEN,
    Color.BLUE
};

ColorStateList myList = new ColorStateList(states, colors);

二、扩展使用

创建自定义 ColorStateListBuilder 类

private class ColorStateListBuilder {
    List<Integer> colors = new ArrayList<>();
    List<int[]> states = new ArrayList<>();

    public ColorStateListBuilder addState(int[] state, int color) {
        states.add(state);
        colors.add(color);
        return this;
    }

    public ColorStateList build() {
        return new ColorStateList(convertToTwoDimensionalIntArray(states),
                convertToIntArray(colors));
    }

    private int[][] convertToTwoDimensionalIntArray(List<int[]> integers) {
        int[][] result = new int[integers.size()][1];
        Iterator<int[]> iterator = integers.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            result[i] = iterator.next();
        }
        return result;
    }

    private int[] convertToIntArray(List<Integer> integers) {
        int[] result = new int[integers.size()];
        Iterator<Integer> iterator = integers.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            result[i] = iterator.next();
        }
        return result;
    }
}

使用 ColorStateListBuilder 类

ColorStateListBuilder builder = new ColorStateListBuilder();
builder.addState(new int[] { android.R.attr.state_pressed }, ContextCompat.getColor(this, colorRes))
       .addState(new int[] { android.R.attr.state_selected }, Color.GREEN)
       .addState(..., some color);

if(// some condition){
      builder.addState(..., some color);
}
builder.addState(new int[] {}, colorNormal); // must add default state at last of all state

ColorStateList stateList = builder.build(); // ColorStateList created here

// textView.setTextColor(stateList);

三、所有状态类型参考

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item  
        android:color="hex_color"  
        android:state_pressed=["true" | "false"]  
        android:state_focused=["true" | "false"]  
        android:state_selected=["true" | "false"]  
        android:state_active=["true" | "false"]  
        android:state_checkable=["true" | "false"]  
        android:state_checked=["true" | "false"]  
        android:state_enabled=["true" | "false"]  
        android:state_window_focused=["true" | "false"] />  
</selector>  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值