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中動(dòng)態(tài)設(shè)置顏色的使用:
TextView 設(shè)置文本顏色支持的兩種類型紊馏,setTextColor(@ColorInt int color)和setTextColor(ColorStateList colors).
View中設(shè)置背景色靠娱,setBackground(Drawable background)、 setBackgroundDrawable(Drawable background)、 setBackgroundColor(@ColorInt int color)和setBackgroundResource(@DrawableRes int resid)浮还,前面兩種使用Drawable類型設(shè)置桑孩,Drawable可通過resource.getDrawable(resId)獲取資源或者自定義Drawable拜鹤,第三種支持ColorInt類型,最后一種可設(shè)置顏色值資源Id流椒,比如setBackgroundResource(R.color.white).