如果我将半透明颜色设置为 cardBackgroundColor,则 cardView 中间会出现奇怪的矩形形状

Strange rectangle shape appears in the middle of cardView if I set semi-transparent color to cardBackgroundColor

我有以下布局。

<androidx.cardview.widget.CardView
        android:layout_width="160dp"
        android:layout_height="160dp"
        app:cardBackgroundColor="#AACC0000"
        app:cardCornerRadius="30dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Some text" />

</androidx.cardview.widget.CardView>

如果我将半透明颜色 (#AACC0000) 设置为 cardBackgroundColor,cardView 中间会出现奇怪的矩形。

如果我设置没有 alpha 通道的颜色 (#CC0000),那个矩形就会消失。问候

添加 alpha 值 android:alpha="0.99" 解决了这个问题。 取 0.99 作为高侧 alpha 值

<androidx.cardview.widget.CardView
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:alpha="0.99"
        app:cardBackgroundColor="#AACC0000"
        app:cardCornerRadius="30dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Some text" />

发生这种情况是因为海拔高度。将 cardElevation 设置为 0dp 并检查

<androidx.cardview.widget.CardView
    android:layout_width="160dp"
    android:layout_height="160dp"
    app:cardBackgroundColor="#AACC0000"
    app:cardCornerRadius="30dp"
    app:cardElevation="0dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Some text" />

</androidx.cardview.widget.CardView>