情况是,当我从程序中修改
Card
视图的背景时,Card
视图的圆角半径会重置。但为什么呢?Card
视图的背景时,Card
视图的圆角半径会重置。但为什么呢?如果您尝试使用带圆角的CardView,并在运行时设置背景色,则会遇到该问题。请改用yourCardView.setCardBackgroundColor()
方法,而不是yourCardView.setBackgroundColor()
方法:)
我找到了一个问题的解决方案。
我需要获取视图的背景并设置其颜色,然后将新的背景分配给该视图。
Drawable backgroundOff = v.getBackground(); //v is a view
backgroundOff.setTint(defaultColor); //defaultColor is an int
v.setBackground(backgroundOff);
这是一个关于IT技术的问题。<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);
祝你好运
RecyclerView
内每个其他CardView
的颜色并保留圆角时,它可以像魔术般运作。在此过程中,您只能将CardView
元素作为android.view.View
itemViews访问,这些元素具有setBackgroundColor()
方法(失败),但没有可用的setCardBackgroundColor()
方法。 - Theta