要动态使用Android的ColorStateList,可以通过代码创建一个ColorStateList对象,并将其应用到View或Drawable对象上。以下是一个示例代码:
// 创建一个ColorStateList对象int[][] states = new int[][] { new int[] {android.R.attr.state_pressed}, new int[] {android.R.attr.state_focused}, new int[] {}};int[] colors = new int[] { Color.RED, Color.GREEN, Color.BLUE};ColorStateList colorStateList = new ColorStateList(states, colors);// 应用ColorStateList到一个Button上Button button = findViewById(R.id.button);button.setTextColor(colorStateList);在这个例子中,我们首先创建一个包含不同状态和颜色的ColorStateList对象。然后,我们将这个ColorStateList对象应用到一个Button的文本颜色上。根据Button的不同状态(按下、获取焦点、普通状态),文本颜色会自动根据ColorStateList中定义的颜色进行切换。


