TextView 中setTextColor 注意

版权声明:本文为博主原创文章,转载请务必注明作者与原文链接。 https://blog.csdn.net/jingerppp/article/details/83186992

一般在设置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));

猜你喜欢

转载自blog.csdn.net/jingerppp/article/details/83186992
今日推荐