首页 > 解决方案 > androidx.cardview.widget.CardView 上的涟漪效应

问题描述

我正在尝试在点击卡片视图时添加涟漪效果,但奇怪的是它没有出现吗?

这里有什么问题?

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="6dp"
    card_view:contentPadding="5dp"
    card_view:cardElevation="4dp"
    card_view:cardMaxElevation="6dp"
    app:ignore="NamespaceTypo"> 


</androidx.cardview.widget.CardView>

// 我有一个线性布局,卡片视图内有三个文本视图。

回收站视图:

<LinearLayout
        android:id="@+id/cardViewLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:visibility="gone">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/cardList"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </LinearLayout>

谢谢!

标签: androidandroid-cardview

解决方案


不要background/foregroundCardView. 如果您使用任何背景颜色,那么只需添加app:cardBackgroundColor="@color/cardBackgroundColor. 从. padding_ CardView用于margin项目之间的空间。

现在,对于 中的涟漪效应CardView,只需在您的CardView. android:background="?attr/selectableItemBackground"在子布局中设置。padding/margin如果需要,可以在孩子中添加任何必要的内容。

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    card_view:cardCornerRadius="6dp"
    card_view:cardElevation="4dp"
    card_view:cardMaxElevation="6dp"
    app:ignore="NamespaceTypo"> 

        <!-- Child Layout -->
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="?attr/selectableItemBackground"
            android:orientation="vertical">

            <!-- Your content here -->
        </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

推荐阅读