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。
最佳答案
你要找的是:
CardView card = ...
card.setCardBackgroundColor(color);
在 XML 中
card_view:cardBackgroundColor="@android:color/white"
更新: 在 XML 中
app:cardBackgroundColor="@android:color/white"
关于android - 以编程方式更改 CardView 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26561122/