我尝试为我的MaterialCardView设置背景颜色,但是我没有得到我期望的结果,我尝试了以下方法:
card.setBackground = R.color.red_color
我想是因为我的MaterialCardView显示的Alpha十六进制数字没有背景:
card.setCardBackgroundColor(0xB00020)
我可以用颜色显示卡的唯一方法是使用此方法,但是MaterialCardView可以透明显示,例如,如果我输入0xFFB00020,Android Studio会抛出错误,因为该函数需要一个整数,但是我不能理解,为什么我的想法会抛出错误?如果假设我将十六进制数作为参数传递:
card.setcardBackgroundColor(0x79B00020)
这是我的代码:
override fun onBindViewHolder(holder: TableHolder, position: Int) {
if (position < tableList.size) {
val table = tableList[position]
val node = holder.table
//Log.e("NODE", node.toString())
holder.guestNumber.text = table.people.toString()
holder.tableName.text = table.description
if (node is MaterialCardView) {
when (table.status) {
"A" -> {
holder.descriptionTable.text = "ABIERTA"
node.setCardBackgroundColor(0xFF7903DAC6)
}
"D" -> {
holder.descriptionTable.text = "DISPONIBLE"
node.setCardBackgroundColor(0x79CA4B02)
}
"C" -> {
holder.descriptionTable.text = "CERRADA"
node.setCardBackgroundColor(0x79FF0266)
}
else -> {
holder.descriptionTable.text = node.context.getString(R.string.error_label)
node.setCardBackgroundColor(0x79B00020)
}
}
}
}
}
第一种情况导致0xFF错误...
编辑
我找到了解决方案!
要在Kotlin中将背景颜色设置为MaterialCardView,必须将十六进制值解析为Int,例如:0xFFCA4B02.toInt()
但是有人可以解释一下为什么在Kotlin中这是必需的吗?
我希望有人能帮助我,
答案 0 :(得分:1)
我刚刚测试过为颜色创建资源并将其作为参数传递,但它没有应用任何透明度:
val cardColor = ContextCompat.getColor(context!!, R.color.card_background)
node.setCardBackgroundColor(cardColor)
答案 1 :(得分:0)
请尝试这样的操作,因为它需要解析颜色。
我添加的颜色是RED
node.setCardBackgroundColor(Color.parseColor("#ffff0000"));