一般在设置TextView 颜色的时候,有人会直接使用:
destroy.setTextColor(android.R.color.holo_red_light);
这是一种错误用法,最终会无法显示正确的颜色。
来看下source code 中如何解释:
/**
* Sets the text color for all the states (normal, selected,
* focused) to be this color.
*
* @param color A color value in the form 0xAARRGGBB.
* Do not pass a resource ID. To get a color value from a resource ID, call
* {@link android.support.v4.content.ContextCompat#getColor(Context, int) getColor}.
*
* @see #setTextColor(ColorStateList)
* @see #getTextColors()
*
* @attr ref android.R.styleable#TextView_textColor
*/
@android.view.RemotableViewMethod
public void setTextColor(@ColorInt int color) {
mTextColor = ColorStateList.valueOf(color);
updateTextColors();
}
这里明确的说明了,参数color 是color value,格式如0xAARRGGBB
所以,上面的例子应该修改为:
destroy.setTextColor(getContext().getColor(android.R.color.holo_red_light));