温馨提示×

android colorstatelist的动态使用方法

小樊
94
2024-06-25 14:14:35
栏目: 编程语言
Android开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要动态使用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中定义的颜色进行切换。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:android colorstatelist使用的技巧

0