以编程方式更改 CardView 的背景颜色 [英] Change the background color of CardView programmatically

查看:72
本文介绍了以编程方式更改 CardView 的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The CardView has an attribute card_view:cardBackgroundColor to define the background color. This attribute works fine.

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

At the same time there isn't a method to change the color dynamically.

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

I've just tried solutions like:

mCardView.setBackgroundColor(...);

或在 cardView 中使用 Layout

or using a Layout inside the 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.

These solutions don't work because the card has a cardCornerRadius.

推荐答案

您要找的是:

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

在 XML 中

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

更新:在 XML 中

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

这篇关于以编程方式更改 CardView 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆