简体   繁体   English

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

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

The situation is that when I modify a Card view background from a program then the corner radius of the Card view reset.的情况是,当我修改Card从程序那么的圆角半径观察背景Card视图复位。 But why?但为什么?
(I don't think I need to provide any other information (code, picture of the result, etc.), because I think it's clear enough to understand. If you need more information then you should write down in a comment.) (我认为我不需要提供任何其他信息(代码,结果图片等),因为我认为它已经足够清楚了。如果您需要更多信息,那么您应该在评论中写下来。)

If you try to use CardView with corner radius you will face this problem when you set background color dynamically.如果您尝试使用具有圆角半径的 CardView,则在动态设置背景颜色时会遇到此问题。 Please use yourCardView.setCardBackgroundColor() methord instead of yourCardView.setBackgroundColor() :)请使用yourCardView.setCardBackgroundColor()方法而不是yourCardView.setBackgroundColor() :)

I found a solution to the question.我找到了问题的解决方案。
I needed to retrieve the background of the view and set its color, then I assigned the new background to the view.我需要检索视图的背景并设置其颜色,然后将新背景分配给视图。

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

(This answer helped: https://stackoverflow.com/a/18394982/9377499 ) (这个答案有帮助: https : //stackoverflow.com/a/18394982/9377499

Same problem and fix the problem this way同样的问题并以这种方式解决问题

At first, create the shape named in Drawable "shape_background_cardview" and this add below code首先,创建在 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>

step second, set the shape to the background CardView yourself第二步,自己将形状设置为背景CardView

yourCardView.setBackgroundResource(R.drawable.shape_background_cardview);

GoodLuck好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM