本文介绍了如何以编程方式创建ColorStateList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此方法以编程方式创建ColorStateList:

I am trying to create a ColorStateList programatically using this:

ColorStateList stateList = new ColorStateList(states, colors);

但是我不确定这两个参数是什么.

But I am not sure what are the two parameters.

根据文档:

public ColorStateList (int[][] states, int[] colors)

已添加到API级别1

Added in API level 1

创建一个ColorStateList,该列表返回从状态到颜色的指定映射.

Creates a ColorStateList that returns the specified mapping from states to colors.

有人可以解释一下如何创建它吗?

Can somebody please explain me how to create this?

二维状态数组的含义是什么?

What is the meaning of two-dimensional array for states?

推荐答案

请参见 http://developer.android.com/reference/android/R.attr.html#state_above_anchor 以获得可用状态列表.

See http://developer.android.com/reference/android/R.attr.html#state_above_anchor for a list of available states.

如果您想为禁用,未聚焦,未选中状态等设置颜色,只需对这些状态取反:

If you want to set colors for disabled, unfocused, unchecked states etc. just negate the states:

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);

这篇关于如何以编程方式创建ColorStateList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 05:02