以编程方式更改 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"