微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CardView:设置背景色可消除圆角

如何解决CardView:设置背景色可消除圆角

我有一个带有圆角的卡片视图:

 <androidx.cardview.widget.CardView
        android:id="@+id/chronology_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        app:cardCornerRadius="12dp">   <------- corner radius set in the layout.

视图显示为带有圆角。 ->好的。

通过代码设置背景色时...

holder.cardView.setBackgroundColor(getBackgroundColor());

...然后圆角消失了。它看起来像是具有正确新颜色的矩形。

如何在运行时设置背景色并保持角落?

解决方法

您可以尝试将其添加到您的活动代码中:

CardView cardView = findViewById(R.id.chronology_card);
cardView.setCardBackgroundColor(Color.parseColor("#put here the HEX code for the color"));

我以前尝试过,并且工作正常,转角半径没有任何问题。

,

CardView管理带有圆角的可绘制背景。
使用setBackgroundColor,您将覆盖此背景。

您应使用方法 setCardBackgroundColor 代替setBackgroundColor

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。