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

9
情况是,当我从程序中修改Card视图的背景时,Card视图的圆角半径会重置。但为什么呢?
3个回答

29

如果您尝试使用带圆角的CardView,并在运行时设置背景色,则会遇到该问题。请改用yourCardView.setCardBackgroundColor()方法,而不是yourCardView.setBackgroundColor()方法:)


6

我找到了一个问题的解决方案。
我需要获取视图的背景并设置其颜色,然后将新的背景分配给该视图。

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

(这个回答有帮助:https://dev59.com/pWMl5IYBdhLWcg3wcmvD#18394982

这是一个关于IT技术的问题。

例如,当您想要更改RecyclerView内每个其他CardView的颜色并保留圆角时,它可以像魔术般运作。在此过程中,您只能将CardView元素作为android.view.View itemViews访问,这些元素具有setBackgroundColor()方法(失败),但没有可用的setCardBackgroundColor()方法。 - Theta

1
Same problem and fix the problem this way. 首先,在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);

祝你好运


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接