阴影未显示在 Androidx Cardview 上

Shadow not showing on Androidx Cardview

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    tools:context=".TurnOnLocation">

    <LinearLayout
        android:id="@+id/requires_location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/turn_on_location">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="4dp"
            app:cardElevation="10dp"
            app:cardPreventCornerOverlap="false">
            <TextView
                android:id="@+id/info_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:paddingStart="60dp"
                android:paddingEnd="60dp"
                android:textSize="28sp"
                android:text="test"
                android:textColor="@color/colorPrimary"
                android:textAlignment="center"
                android:background="@color/colorPrimaryDark" />

        </androidx.cardview.widget.CardView>
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/turn_on_location"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/medium_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/turn_on_location"
        app:layout_constraintTop_toBottomOf="@id/requires_location"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我指定了 app:cardElevation="10dp",我认为它是用来渲染阴影的。但是没有影子出现。

知道为什么吗?

将以下行添加到 CardView 标签中

  card_view:cardBackgroundColor="@android:color/white"
  card_view:cardElevation="2dp"
  card_view:cardUseCompatPadding="true"

试试这个

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="4dp"
        app:cardElevation="10dp"
        android:layout_margin="16dp">
</androidx.cardview.widget.CardView

将此行添加到您的 AndroidManifest android:hardwareAccelerated="true" application 标签

你的源代码没有错。应该正确渲染阴影。您可能正在检查布局预览中的输出。它在渲染上有一些问题。请运行模拟器或真实设备中的应用程序。

使用此代码,对我来说很有效!这个想法是 app:cardUserCompatPadding="true"

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="4dp"
    android:layout_marginVertical="@dimen/two_x"
    app:cardBackgroundColor="@color/white">

结果