150

CardView一个属性card_view:cardBackgroundColor来定义背景颜色。此属性工作正常。

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

我刚刚尝试了以下解决方案:

mCardView.setBackgroundColor(...);

或在 cardView 内使用 Layout

   <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。

4

20 回答 20

299

您正在寻找的是:

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

在 XML 中

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

更新:在 XML

app:cardBackgroundColor="@android:color/white"
于 2014-11-24T08:05:10.390 回答
115

使用属性 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"
    >
于 2015-03-19T00:17:40.420 回答
30

您可以在 XML 中使用它

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

或者这个在Java中

cardView.setCardBackgroundColor(Color.WHITE);
于 2015-04-09T12:16:34.023 回答
19

我使用此代码以编程方式设置:

card.setCardBackgroundColor(color);

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

card_view:cardBackgroundColor="@android:color/white"
于 2015-04-10T11:21:27.153 回答
12

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

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

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

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

于 2017-11-02T14:06:34.240 回答
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.

似乎没有更好的方法来做到这一点。

于 2014-10-25T10:14:33.580 回答
9

你可以在下面使用

cardview.setBackgroundColor(Color.parseColor("#EAEDED"));
于 2019-06-06T11:12:20.693 回答
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'
 }

希望这可以帮助。快乐编码

于 2015-06-23T22:11:00.123 回答
4

这在 kotlin 中非常简单。使用 ColorStateList 改变卡片视图颜色

   var color = R.color.green;
   cardView.setCardBackgroundColor(context.colorList(color));

ColorStateList 的 kotlin 扩展:

fun Context.colorList(id: Int): ColorStateList {
    return ColorStateList.valueOf(ContextCompat.getColor(this, id))
}
于 2019-10-17T09:38:35.890 回答
3

JAVA

cardView.setCardBackgroundColor(0xFFFEFEFE);

android 使用 ARGB 颜色。你可以像这样使用(0xFF + RGB 颜色)——硬编码颜色。

于 2016-02-11T20:31:48.853 回答
3

在 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

于 2020-05-28T22:01:29.270 回答
2

我在尝试以编程方式创建卡片视图时遇到了同样的问题,奇怪的是查看文档https://developer.android.com/reference/android/support/v7/widget/CardView.html#setCardBackgroundColor%28int %29,谷歌的人公开了改变卡片视图背景颜色的api,但奇怪的是我没有成功在支持库中访问它,所以这对我有用:

CardViewBuilder.java

    mBaseLayout = new FrameLayout(context);
    // FrameLayout Params
    FrameLayout.LayoutParams baseLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    mBaseLayout.setLayoutParams(baseLayoutParams);

    // Create the card view.
    mCardview = new CardView(context);
    mCardview.setCardElevation(4f);
    mCardview.setRadius(8f);
    mCardview.setPreventCornerOverlap(true); // The default value for that attribute is by default TRUE, but i reset it to true to make it clear for you guys
    CardView.LayoutParams cardLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    cardLayoutParams.setMargins(12, 0, 12, 0);
    mCardview.setLayoutParams(cardLayoutParams);
    // Add the card view to the BaseLayout
    mBaseLayout.addView(mCardview);

    // Create a child view for the cardView that match it's parent size both vertically and horizontally
    // Here i create a horizontal linearlayout, you can instantiate the view of your choice
    mFilterContainer = new LinearLayout(context);
    mFilterContainer.setOrientation(LinearLayout.HORIZONTAL);
    mFilterContainer.setPadding(8, 8, 8, 8);
    mFilterContainer.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER));

    // And here is the magic to get everything working
    // I create a background drawable for this view that have the required background color
    // and match the rounded radius of the cardview to have it fit in.
    mFilterContainer.setBackgroundResource(R.drawable.filter_container_background);

    // Add the horizontal linearlayout to the cardview.
    mCardview.addView(mFilterContainer);

filter_container_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="@android:color/white"/>

这样做我成功地保持了cardview阴影和圆角。

于 2014-11-27T17:52:12.410 回答
2

我在Xamarin.Android - VS (2017)上遇到了同样的问题

对我有用的解决方案

解决方案

在您的 XML 文件中添加:

 xmlns:card_view="http://schemas.android.com/apk/res-auto"

并在您的 android.support.v7.widget.CardView元素中添加此礼节:

card_view:cardBackgroundColor="#ffb4b4"

(IE)

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="12dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="1dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardBackgroundColor="#ffb4b4" />

您还可以添加cardElevationcardElevation

如果你想以cardview 编程方式编辑你只需要使用这个代码: For (C#)

    cvBianca = FindViewById<Android.Support.V7.Widget.CardView>(Resource.Id.cv_bianca);
    cvBianca.Elevation = 14;
    cvBianca.Radius = 14;
    cvBianca.PreventCornerOverlap = true;
    cvBianca.SetCardBackgroundColor(Color.Red);

现在您可以通过编程方式更改背景颜色,而不会丢失边框、角半径和高度。

于 2017-07-23T13:15:59.630 回答
1

你可以在java中使用它。

cardView.setCardBackgroundColor(Color.parseColor("#cac8a0"));

代码颜色表格http://www.color-hex.com/

于 2018-08-27T04:24:09.513 回答
1

我终于得到了留下来的角落。这是 c#,Xamarin.Android

在 ViewHolder 中:

CardView = itemView.FindViewById<CardView>(Resource.Id.cdvTaskList);

在适配器中:

vh.CardView.SetCardBackgroundColor(Color.ParseColor("#4dd0e1"));
于 2019-05-10T06:01:51.623 回答
1

对我来说最简单的方法是这个(Kotlin)

card_item.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#fc4041"))
于 2020-03-04T12:40:53.983 回答
0

Cardview有点腼腆。我的结构中有颜色列表,模型就像

class ModelColor : Serializable {

var id: Int? = 0
var title: String? = ""
var color: Int? = 0// HERE IS THE COLOR FIELD WE WILL USE

constructor(id: Int?, title: String?, color: Int?) {
    this.id = id
    this.title = title
    this.color = color
}

}

用颜色加载模型,构造上的最后一项取自R.color

 list.add(ModelColor(2, getString(R.string.orange), R.color.orange_500))

最后你可以 setBackgrıundResource

 cv_add_goal_choose_color.setBackgroundResource(color)
于 2018-10-11T08:57:33.220 回答
0

您可以在 XML 中使用它

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

或以编程方式

cardView.setCardBackgroundColor(ContextCompat.getColor(this, R.color.my_color));
于 2021-06-03T17:45:09.180 回答
0

如果你想改变卡片的色调,试试这个代码,它对我有用。对我来说,这段代码有效。我将它与卡片和图像视图一起使用,但我认为它可以在任何视图中更改其色调颜色。cardBookmark 是我的 cardView 名称。

var cardDrawable: Drawable = binding.cardBookmark.background
cardDrawable = DrawableCompat.wrap(cardDrawable)
DrawableCompat.setTint(cardDrawable, resources.getColor(R.color.shuffleColor))
binding.cardBookmark.background = cardDrawable
于 2021-07-01T22:07:40.583 回答
-1

试试看很简单

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardBackgroundColor="#fff"
    card_view:cardCornerRadius="9dp"
    card_view:cardElevation="4dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">
于 2017-04-15T13:57:59.383 回答