【发布时间】:2018-09-24 23:26:28
【问题描述】:
情况是,当我从程序修改Card 视图背景时,Card 视图的角半径会重置。但是为什么呢?
(我认为我不需要提供任何其他信息(代码、结果图片等),因为我认为它足够清晰,可以理解。如果您需要更多信息,那么您应该写在评论中。)
【问题讨论】:
标签: android view background android-cardview cornerradius
情况是,当我从程序修改Card 视图背景时,Card 视图的角半径会重置。但是为什么呢?
(我认为我不需要提供任何其他信息(代码、结果图片等),因为我认为它足够清晰,可以理解。如果您需要更多信息,那么您应该写在评论中。)
【问题讨论】:
标签: android view background android-cardview cornerradius
如果您尝试使用具有圆角半径的 CardView,您将在动态设置背景颜色时遇到此问题。请使用yourCardView.setCardBackgroundColor() 方法而不是yourCardView.setBackgroundColor() :)
【讨论】:
我找到了问题的解决方案。
我需要检索视图的背景并设置其颜色,然后将新背景分配给视图。
Drawable backgroundOff = v.getBackground(); //v is a view
backgroundOff.setTint(defaultColor); //defaultColor is an int
v.setBackground(backgroundOff);
【讨论】:
RecyclerView 内的所有其他 CardView 的颜色并保留圆角时,它就像一个魅力。在此过程中,您只能以android.view.View itemViews 的形式访问CardView 元素,这些元素具有setBackgroundColor() 方法(失败)但没有setCardBackgroundColor() 方法可用。
同样的问题,用这种方法解决问题
首先,创建名为 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);
祝你好运
【讨论】: