【问题标题】:CardView: Setting background color removes the rounded edgesCardView:设置背景颜色会删除圆角边缘
【发布时间】:2020-10-22 17:15:28
【问题描述】:

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

 <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());

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

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

【问题讨论】:

    标签: android android-cardview


    【解决方案1】:

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

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

    我之前试过,效果很好,拐角半径没有任何问题。

    【讨论】:

      【解决方案2】:

      CardView 管理自己的圆角背景可绘制对象。
      使用setBackgroundColor,您将覆盖此背景。

      您应该使用方法 setCardBackgroundColor 而不是 setBackgroundColor

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        • 2012-01-05
        • 1970-01-01
        相关资源
        最近更新 更多