【问题标题】:Get Color from resource id integer gives error从资源 id 整数获取颜色给出错误
【发布时间】:2019-01-24 07:39:36
【问题描述】:

我创建了一个自定义视图,该视图接受背景颜色残留和阴影颜色 resId。但是当我转换资源 id 以将它们存储在 int 变量中时。它给出的错误为

Expected a color resource id (R.color.) but received an RGB integer 

以下是我的设置方法

    @Override
    public void setBackgroundColor(@ColorRes int backgroundResId) {
        this.backgroundColor = ContextCompat.getColor(context, backgroundResId);
    }

    public void setShadowColor(@ColorRes int shadowColorId) {
        this.shadowColor = ContextCompat.getColor(context, shadowColorId);
    }

使用时出现同样的错误

public void setBackgroundColor(@ColorRes int backgroundResId) {
    this.backgroundColor = context.getResources().getColor(backgroundResId);
}

【问题讨论】:

  • 你能举个例子说明什么是作为“backgroundResId”传递的吗?
  • 你能分享你调用setBackgroundColor()方法的代码吗

标签: android android-view android-resources android-context


【解决方案1】:

我想你在方法setBackgroundColor 中传递了错误的参数。你应该这样使用它:

//some code
setBackgroundColor(R.color.colorPrimary)
//some code

【讨论】:

  • getColor 已弃用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多