当我更改卡片视图的背景时,角半径被重置

When I change the background of a Card view, the corner radius is reset

情况是,当我从程序修改 Card 视图背景时,Card 视图的角半径会重置。但是为什么?
(我认为我不需要提供任何其他信息(代码,结果图片等),因为我认为它足够清楚易懂。如果您需要更多信息,那么您应该写在一个评论。)

我找到了问题的答案。
我需要检索视图的背景并设置其颜色,然后我将新背景分配给视图。

Drawable backgroundOff = v.getBackground(); //v is a view
backgroundOff.setTint(defaultColor); //defaultColor is an int 
v.setBackground(backgroundOff);

(此回答有帮助:)

如果您尝试使用带圆角半径的 CardView,则在动态设置背景颜色时会遇到此问题。请使用 yourCardView.setCardBackgroundColor() 方法而不是 yourCardView.setBackgroundColor() :)

同样的问题,这样解决问题

首先,创建在 Drawable "shape_background_cardview" 中命名的形状,并在下面添加代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="@color/gray500" />
<corners android:radius="5dp" />

</shape>

第二步,自己设置形状为背景CardView

yourCardView.setBackgroundResource(R.drawable.shape_background_cardview);

祝你好运