Android 中代码定义颜色的几种方式:

1. Color.parseColor("#FFFFFF"),返回类型ColorInt.

2. Color.rgb(255,255,255),返回类型ColorInt.

3. Resources resource = (Resources) getResources();

ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.white);


在TextView与View中动态设置颜色的使用:

TextView 设置文本颜色支持的两种类型,setTextColor(@ColorInt int color)和setTextColor(ColorStateList colors).

View中设置背景色,setBackground(Drawable background)、 setBackgroundDrawable(Drawable background)、 setBackgroundColor(@ColorInt int color)和setBackgroundResource(@DrawableRes int resid),前面两种使用Drawable类型设置,Drawable可通过resource.getDrawable(resId)获取资源或者自定义Drawable,第三种支持ColorInt类型,最后一种可设置颜色值资源Id,比如setBackgroundResource(R.color.white).



链接:https://www.jianshu.com/p/1d47526253ee



猜你喜欢

转载自blog.csdn.net/yijiaodingqiankun/article/details/80727890