Android CardView背景色在API 21中显示为红色

Android CardView背景色在API 21中显示为红色,android,android-layout,Android,Android Layout,我有一个CardView,它可以根据激活状态使用选择器drawable更改cardBackgroundColor。这在API25和30仿真器中运行良好,但在API21仿真器中,背景颜色为红色,不会改变。我没有任何红色的自定义颜色 卡德维尤 <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

我有一个CardView,它可以根据激活状态使用选择器drawable更改cardBackgroundColor。这在API25和30仿真器中运行良好,但在API21仿真器中,背景颜色为红色,不会改变。我没有任何红色的自定义颜色

卡德维尤

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="6dp"
android:layout_marginTop="8dp"
app:cardBackgroundColor="@drawable/list_item_note_background"
app:cardCornerRadius="4dp">

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/note_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="16sp" />

</androidx.cardview.widget.CardView>

可牵引

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_activated="true" />
<item android:color="?attr/colorSurface" />
</selector>

原色为#04d4f0
表面颜色是#FFFFFF

问题在于您的可绘制性,我希望colorPrimary和colorSurface已经存在于您的colors.xml文件中,在这里您通过ATTR访问颜色,ATTR将选择android操作系统提供的可绘制性下面的颜色,希望对您有用

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_activated="true" />
    <item android:color="@color/colorSurface" />
</selector>


?attr/colorPrimary
表示主题中使用属性定义的颜色
colorPrimary
@GabrieleMariotti:知道了,但我认为颜色表面是从操作系统提供的值中选择的,这就是为什么它会变红谢谢,这很有效,但为什么我必须为API 21而不是其他的颜色这么做?