侧边栏壁纸
博主头像
Eoser's page! 博主等级

@学习@生活@自己

  • 累计撰写 114 篇文章
  • 累计创建 29 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Android 代码中设置字体颜色无效

eoser
2023-03-23 / 0 评论 / 0 点赞 / 0 阅读 / 0 字

其实是代码中设置错误

setTextColor(int color)误导输入了颜色的资源,因为R.color.alert_dialog_button_disable_color也是个int值

**
     * 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();
    }

可以从上面的源码看到,颜色是值得一个0xAARRGGBB的int值,而R.color获取到的是一个颜色的id值

View.setTextColor(填充颜色);
ins.setTextColor(R.color.alert_dialog_button_disable_color); 错误的调用方法

ins.setTextColor(getResources().getColor(R.color.alert_dialog_button_disable_color));正确的调用方法**粗体**
0

评论区