MaterialCardView如何在程序中动态更改背景颜色和描边颜色

3

专业开发人员。我想尝试通过程序来更改卡片视图的背景和描边颜色,但每次使用card.setCardBackgroundColor()时都会覆盖描边颜色。在XML静态设置中,它可以正常工作。但是通过程序设置会产生这样的效果。

val colorWithAlpha = ColorUtils.setAlphaComponent(tagColor, 128)
tagView.card.setStrokeColor(ColorStateList.valueOf(colorWithAlpha))
tagView.card.setCardBackgroundColor(ColorStateList.valueOf(tagColor))

同样适用于:card.backgroundTintList = ColorStateList.valueOf(tagColor)

XML:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#DB8A61"
    app:cardCornerRadius="8dp"
    app:cardElevation="0dp"
    app:cardUseCompatPadding="true"
    app:cardPreventCornerOverlap="true"
    app:strokeColor="#EBD3C7"
    app:strokeWidth="4dp">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tag_text"
        style="@style/SmallLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:lines="1"
        android:paddingStart="8dp"
        android:paddingTop="4dp"
        android:paddingEnd="8dp"
        android:paddingBottom="4dp"
        android:textColor="@color/white"
        app:autoSizeMaxTextSize="100sp"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeStepGranularity="1sp"
        app:autoSizeTextType="uniform"
        tools:text="Some Text" />

</com.google.android.material.card.MaterialCardView>

看起来是这样的: XML:enter image description here 在代码中实现:
private fun createAddTagView(tagName: String, tagColor: Int, container: ViewGroup) {
        val tagView = LayoutTagBinding.inflate(inflater, container, true)
        tagView.tagText.text = tagName
        val colorWithAlpha = ColorUtils.setAlphaComponent(tagColor, 128)
        tagView.card.setStrokeColor(ColorStateList.valueOf(colorWithAlpha))
        tagView.card.setCardBackgroundColor(ColorStateList.valueOf(tagColor))
    }

enter image description here

当移除时:tagView.card.setCardBackgroundColor(ColorStateList.valueOf(tagColor))

enter image description here

当没有编程更改(颜色由XML设置)时,看起来像预期的:

enter image description here

如何使其显示为XML中的样式?

1个回答

4

我认为问题不在 MaterialCardView 的背景/描边方法上;而是布局中使用的颜色阴影与编程中使用的颜色阴影不同。

布局中使用的颜色:

app:cardBackgroundColor="#DB8A61"
app:strokeColor="#EBD3C7"

但是在编程中,您需要使用tagColor并使用colorWithAlpha添加alpha通道;您没有提供tagColor;但是添加128 alpha组件不会改变太多;这与没有alpha组件的布局颜色完全不同。

因此,要以编程方式具有相同的颜色,只需重用没有alpha组件的相同布局颜色:

tagView.card.setCardBackgroundColor(ColorStateList.valueOf(Color.parseColor("#DB8A61")))
tagView.card.setStrokeColor(ColorStateList.valueOf(Color.parseColor("#EBD3C7")))

谢谢你的回答。似乎strokeColor=没有应用alpha通道颜色。你有什么想法,如何使来自DB8A61的颜色与带有alpha但不带有alpha EBD3C7相同? - billy gates
@billygates,你可以使用添加前两个十六进制数字来表示不透明度的颜色,而不是使用alpha组件方法;因此整个颜色将由8个数字组成,而不是6个。类似于#80EBD3C7而不是#EBD3C7;并在答案中使用相同的过程。 - Zain
setStrokeColor 无法与 alpha 通道一起使用。 - billy gates
不确定...但让我们检查文档...可能是一个错误。 - Zain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接