以编程方式将特定颜色设置为ColorStateList

9

我从我的问题 Add Color "#e3bb87" to StateListDrawable programmatically 创建了StateListDrawable,但是 TextView.setTextColor 不接受 StateListDrawable(奇怪的是在布局中却可以),而是接受 ColorStateList。我阅读了这个change statelistdrawable text color android button

在 ColorStateList 的构造函数中,它仅接受 int 数组。

ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{R.attr.state_pressed},
                    new int[]{R.attr.state_selected},
                    new int[]{-R.attr.state_selected},
            },
            new int[]{
                    Color.GREEN,
                    Color.BLUE,
                    Color.RED});

颜色没有在 colors.xml 中定义,因为我下载了这个颜色属性。怎样才能像这样定义呢?
ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{R.attr.state_pressed}
            },
            **getThisColor**("#e3bb87"));
2个回答

11

使用此功能

ColorStateList colorStateList = new ColorStateList(
            new int[][] { new int[] { R.dimen.padding_large } },
            new int[] {Color.parseColor("#e3bb87")});

幸运的是我第一个完成了。 :D :) - AnujMathur_07
8
dimen.padding_large在这里的意义是什么? - kos

10

您可以使用ColorStateListvalueOf()方法,该方法返回一个包含单一颜色的ColorStateList

ColorStateList.valueOf(Color.parseColor("#e3bb87"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接