如何以编程方式创建ColorStateList?

编程入门 行业动态 更新时间:2024-10-08 18:31:30
本文介绍了如何以编程方式创建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?

推荐答案

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

See developer.android/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?

本文发布于:2023-11-28 11:16:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   ColorStateList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!