我正在尝试使用 RecycleView Adapter 中资源的颜色
override fun onBindViewHolder(holder: NavlogViewHolder, position: Int) {
holder.myTextView.setBackgroundColor(R.color.magenta)
这给出了一个错误:“应该传递解析的颜色而不是资源 ID”并且颜色不是应该的。这也是错误的:
holder.myTextView.setBackgroundColor(getResources.getColor(R.color.magenta))
holder.myTextView.setBackgroundColor(context.resources.getColor(R.color.magenta))
我可以通过制作局部变量来获得颜色:
val color = "#f7f7f7"
holder.myTextView.setBackgroundColor(Color.parseColor(color))
但我更想从 colors.xml 中获取颜色如何正确执行?
最佳答案
val myColor = ContextCompat.getColor(holder.myTextView.context, R.color.magenta)
holder.myTextView.setBackgroundColor(myColor)
关于android - 如何在 RecyclerView 中使用 getColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70041582/