CardView:设置背景色可消除圆角

时间:2020-10-22 17:15:28

标签: android android-cardview

我有一个带有圆角的卡片视图:

 <androidx.cardview.widget.CardView
        android:id="@+id/chronology_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        app:cardCornerRadius="12dp">   <------- corner radius set in the layout.

视图显示为带有圆角。 ->好的。

通过代码设置背景色时...

holder.cardView.setBackgroundColor(getBackgroundColor());

...然后圆角消失了。它看起来像是具有正确新颜色的矩形。

如何在运行时设置背景色并保持角落?

2 个答案:

答案 0 :(得分:3)

您可以尝试将其添加到您的活动代码中:

CardView cardView = findViewById(R.id.chronology_card);
cardView.setCardBackgroundColor(Color.parseColor("#put here the HEX code for the color"));

我以前尝试过,并且工作正常,转角半径没有任何问题。

答案 1 :(得分:1)

CardView管理带有圆角的可绘制背景。
使用setBackgroundColor,您将覆盖此背景。

您应使用方法 setCardBackgroundColor 代替setBackgroundColor

相关问题