改变CardView的背景色编程背景色、CardView

2023-09-04 10:54:45 作者:就算在痛、还是继续微笑。

借助 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内布局

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);