我正在尝试使用parseColor设置TextView背景颜色,但是我收到此错误:
期望颜色资源ID,但收到RGB颜色整数。
有人可以帮我吗?
textView.setBackgroundColor(ContextCompat.getColor(itemView.context, Color.parseColor(product.brand.color)))
对象product.brand.color =#123123(这是示例颜色)
答案 0 :(得分:1)
替换:
textView.setBackgroundColor(ContextCompat.getColor(itemView.context, Color.parseColor(product.brand.color)))
具有:
textView.setBackgroundColor(Color.parseColor(product.brand.color))
ContextCompat.getColor()
返回与颜色资源关联的颜色(例如R.color.primary
)。 Color.parseColor()
不返回颜色资源ID,这就是您收到错误的原因。相反,Color.parseColor()
返回实际颜色,无论如何,这是您想要setBackgroundColor()
的颜色。
答案 1 :(得分:0)
textView.setBackgroundColor(getResources().getColor(R.color.yourcolor));
从资源中解析颜色。