cardview 未将背景设置为透明

cardview not setting background to transparent

您好,我有一个位于视图中心的卡片视图,我想让它的背景透明。但它似乎没有这样做并且有白色背景

这是我的xml

<android.support.v7.widget.CardView
    android:id="@+id/boundaryBox"
    android:layout_width="270dp"
    android:layout_height="176dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardElevation="0dp"
    card_view:cardCornerRadius="14dp" />

你检查过你的父布局有没有背景,因为如果你尝试下面的代码,cardview 将是透明的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:padding="@dimen/dp_16"
    tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/boundaryBox"
        android:layout_width="match_parent"
        android:layout_height="176dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardElevation="16dp"
        app:cardCornerRadius="14dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="test"/>
    </android.support.v7.widget.CardView>
</RelativeLayout>

请试试这个。