以编程方式更改CardView的背景颜色

160

CardView具有属性card_view:cardBackgroundColor来定义背景颜色。该属性正常工作。

同时,没有动态更改颜色的方法。

我刚刚尝试了一些解决方案:

mCardView.setBackgroundColor(...);

或者在cardView内使用布局

   <android.support.v7.widget.CardView>
        <LinearLayout
            android:id="@+id/inside_layout">
    </android.support.v7.widget.CardView>  

 View insideLayout = mCardView.findViewById(R.id.inside_layout);
 cardLayout.setBackgroundColor(XXXX);

这些解决方案行不通,因为该卡片拥有一个cardCornerRadius。

27个回答

314

你正在寻找的是:

CardView card = ...
card.setCardBackgroundColor(color);

在XML中

 card_view:cardBackgroundColor="@android:color/white"

更新:在XML中

app:cardBackgroundColor="@android:color/white"

7
似乎是支持库中的一种新方法。上个月我没有看到它,可能是一次更新。 - Gabriele Mariotti
5
可能需要补充说明的是,在XML中配置这个的方法是 card_view:cardBackgroundColor="@android:color/white" - Mavamaarten
3
使用 card_view 命名空间对我无效,我必须使用 app 代替。 - rsicarelli
1
@rsicarelli 一切都取决于你给它命名的方式:xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/apk/res-auto" - Bryan Bryce
1
我该如何在程序中将Gradient Drawable设置为卡片背景? - Bhaumik Ghodasara

116

使用属性 card_view:cardBackgroundColor:

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    card_view:cardCornerRadius="4dp"
    android:layout_margin="10dp"
    card_view:cardBackgroundColor="#fff"
    >

2
OP在他的问题的第一行告诉我们关于那个属性,他说他想要“动态地”实现它。 - stramin

33

您可以在XML中使用此功能。

card_view:cardBackgroundColor="@android:color/white"

或者在Java中这样写

cardView.setCardBackgroundColor(Color.WHITE);

21

我使用这段代码进行编程设置:

card.setCardBackgroundColor(color);

或者在XML中,您可以使用以下代码:

card_view:cardBackgroundColor="@android:color/white"

这个答案更有用,因为它包含了静态的(XML)和编程的(JAVA)两种方式。赞。 - chntgomez

16

我在recylerView中格式化CardViews时遇到了类似的问题。

我找到了这个简单的解决方案,不确定它是否是最好的解决方案,但对我有效。

mv_cardView.getBackground().setTint(Color.BLUE)

它获取CardView的背景Drawable并对其进行着色。


12

您可以使用以下内容:

cardview.setBackgroundColor(Color.parseColor("#EAEDED"));

10

initialize方法中设置的方式使用了受保护的RoundRectDrawable类,代码如下:

RoundRectDrawable backgroundDrawable = new RoundRectDrawable(backgroundColor, cardView.getRadius());
cardView.setBackgroundDrawable(backgroundDrawable);

这并不美观,但是您可以扩展那个类。类似于:

package android.support.v7.widget;

public class MyRoundRectDrawable extends RoundRectDrawable {

    public MyRoundRectDrawable(int backgroundColor, float radius) {
        super(backgroundColor, radius);
    }

}

然后:

final MyRoundRectDrawable backgroundDrawable = new MyRoundRectDrawable(bgColor,
            mCardView.getRadius());
mCardView.setBackgroundDrawable(backgroundDrawable);

编辑

在 < API 21 中,这样做将不会给你阴影效果,因此你需要使用 RoundRectDrawableWithShadow 来完成同样的操作。

似乎没有更好的方法来实现这个。


2
+1 对于黑客行为,我表示赞同。我找不到更好的解决方案。 无论如何,我希望官方API中有不同的选择。 - Gabriele Mariotti
这个类(RoundRectDrawableWithShadow或RoundRectDrawable)是可访问的吗?我认为不是。 - Napolean

8

稍微有些晚了,而且这与编程无关。但我觉得最好为小部件设置样式,你可以为CardView创建一个样式,这将使你的xml代码更加简洁...

<style name="MyCardViewStyle" parent="CardView">
    <!-- Card background color -->
    <item name="cardBackgroundColor">@android:color/white</item>
    <!-- Ripple for API 21 of android, and regular selector on older -->
    <item name="android:foreground">?android:selectableItemBackground</item>
    <!-- Resting Elevation from Material guidelines -->
    <item name="cardElevation">2dp</item>
    <!-- Add corner Radius -->
    <item name="cardCornerRadius">2dp</item>
    <item name="android:clickable">true</item>
    <item name="android:layout_margin">8dp</item>
</style>

这是使用 android.support.v7.widget.CardView,然后在布局文件中设置样式:
 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.CardView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     style="@style/MyCardViewStyle">
    <!-- Other fields-->
 </android.support.v7.widget.CardView>

您需要通过gradle在Android Studio中导入appcompat-v7库:

 dependencies {
     compile 'com.android.support:appcompat-v7:22.2.0'
 }

希望这能帮到你。愉快编码。

1
感谢指出XML样式问题。我总是在这方面挣扎。难道没有一种方法可以在themes.xml中定义样式吗?那么您就不必为每个CardView设置样式了。 - Wirling
这真的很棒 - 我一直在努力尝试弄清楚如何通过布局传播选择器 - 但在API21之前这是不起作用的。涟漪在API21后运行良好,但您的主题方法是我第一次成功地使CardView在单击内容时呈现正确的外观。 - Jim Andreas
OP在他的问题的第一行告诉我们关于那个属性,他说他想要“动态地”实现它。 - stramin

6
使用Kotlin非常简单。使用ColorStateList来改变卡片视图的颜色。
   var color = R.color.green;
   cardView.setCardBackgroundColor(context.colorList(color));

一个使用 Kotlin 扩展实现的 ColorStateList:
fun Context.colorList(id: Int): ColorStateList {
    return ColorStateList.valueOf(ContextCompat.getColor(this, id))
}

4
在 Kotlin 中,我可以像这样更改背景颜色:
var card: CardView = itemView.findViewById(com.mullr.neurd.R.id.learn_def_card)
card.setCardBackgroundColor(ContextCompat.getColor(main,R.color.selected))

如果您想要移除颜色,您可以这么做:

card.setCardBackgroundColor(Color.TRANSPARENT)

使用这种方法,我成功创建了一种选择动画。
https://gfycat.com/equalcarefreekitten


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