英文:
Setting strokeColor of MaterialCardView outline makes it go grey no matter what
问题
Here's the translated content:
我有一个用于RecyclerView的MaterialCardView,每个元素都有其重要性,每个元素的轮廓应该是相应的颜色。
这是我的XML代码:
<com.google.android.material.card.MaterialCardView
app:strokeColor="@color/low_importance_green"
app:strokeWidth="@dimen/cardview_border_width"
app:rippleColor="@color/low_importance_green">
以下是应设置颜色的代码部分:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val colour = when (viewModel.importance){
1 -> R.color.high_importance_red
2 -> R.color.medium_importance_yellow
else -> R.color.low_importance_green
}
holder.card.setRippleColorResource(colour)
holder.card.strokeColor = colour
}
仅通过XML应用strokeColor(而不更改颜色)可以正常工作:
但是使用onBindViewHolder()来更改strokeColor时,会出现以下问题:
而且,要确保为CardView设置的涟漪颜色正常工作且没有问题。
这种行为可能是由什么引起的?
我有一个猜测,可能与应用的MaterialTheme有关,因为灰色具有一些非常轻微的紫色成分。但是为什么会这样,我该如何修复呢?
英文:
I have a MaterialCardView for a RecyclerView, each element has its importance and the outline of each element should be the corresponding colour.
Here is my XML:
<com.google.android.material.card.MaterialCardView
app:strokeColor="@color/low_importance_green"
app:strokeWidth="@dimen/cardview_border_width"
app:rippleColor="@color/low_importance_green">
And here the code where the colour should be set:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val colour = when (viewModel.importance){
1 -> R.color.high_importance_red
2 -> R.color.medium_importance_yellow
else -> R.color.low_importance_green
}
holder.card.setRippleColorResource(colour)
holder.card.strokeColor = colour
}
Simply applying the strokeColor in xml (and not changing colour) works fine:
But using the onBindViewHolder() to change the strokeColor, I get this:
IMAGE: setting via code individually fails
And just to be sure, the ripple colour that is set for the cardview works fine and has no issues.
What could lead to this behaviour?
I have one guess, which might be something about the MaterialTheme being applied because the grey has some very slight purple to it. But why should it and how do i fix that?
答案1
得分: 0
Sure, here is the translated code snippet:
请尝试这样做:
val colour = when (viewModel.importance){
1 -> R.color.high_importance_red
2 -> R.color.medium_importance_yellow
else -> R.color.low_importance_green
}
holder.card.setStrokeColor(ContextCompat.getColor(this, colour))
英文:
Please try this:
val colour = when (viewModel.importance){
1 -> R.color.high_importance_red
2 -> R.color.medium_importance_yellow
else -> R.color.low_importance_green
}
holder.card.setStrokeColor(ContextCompat.getColor(this, colour))
答案2
得分: 0
问题出在你如何使用API上。
如果你查看MaterialCardView#setStrokeColor的源代码,你会看到这个方法需要@ColorInt
。@ColorInt
表示这个变量代表了一个打包的颜色整数,AARRGGBB
。
然而,在你的代码中,你引用了一个资源地址(也就是@ColorRes
)。这是一个颜色资源引用(例如 android.R.color.black
)。
val colour = when (viewModel.importance){
1 -> R.color.high_importance_red
2 -> R.color.medium_importance_yellow
else -> R.color.low_importance_green
}
只要资源(@ColorRes
)和实际颜色值(@ColorInt
)都是整数,这个方法就能正常工作,但它实际上做的事情可能与你期望看到的完全不同。
要将你的“引用”转换为“颜色”,你需要从资源中提取它,使用Resources#getColor
方法。
幸运的是,还有一个兼容版本 ContextCompat#getColor
。
要修复这个问题,你需要将
holder.card.strokeColor = colour
改为
holder.card.strokeColor = ContextCompat.getColor(holder.card.context, colour)
英文:
The problem is in how you use the API.
If you take a look at the source code of MaterialCardView#setStrokeColor you will see that the method requires @ColorInt
. @ColorInt
means that the variable represents a packed color int, AARRGGBB
.
However, in your code you are referring to a resource address (aka @ColorRes
). This is a color resource reference (e.g. android.R.color.black
).
val colour = when (viewModel.importance){
1 -> R.color.high_importance_red
2 -> R.color.medium_importance_yellow
else -> R.color.low_importance_green
}
As long as both the resource (@ColorRes
) and the actual color value (@ColorInt
) are both integers
the method is working but doing a completely different thing from what you expected to see.
To convert your reference
to color
you need to extract it from the resources using Resources#getColor
method.
Luckily, there is a compat
version ContextCompat#getColor
To fix the problem you need to change
holder.card.strokeColor = colour
with
holder.card.strokeColor = ContextCompat.getColor(holder.card.context, colour)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论