CardView 在 RecyclerView 中的阴影效果无法显示

5

CardView的高度在单独应用程序中测试正常,但是当使用相同的CardView代码来创建RecyclerView列表项时,高度不再显示。

这是RecyclerView列表项的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
             android:layout_width="match_parent"
             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/apk/res-auto"
             android:layout_margin="10dp">


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    card_view:cardUseCompatPadding="true"
    app:cardMaxElevation="6dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="24dp">

    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            />
    </LinearLayout>


</android.support.v7.widget.CardView>

这是我的主要活动xml文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   xmlns:tools="http://schemas.android.com/tools"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   android:background="@drawable/bitmap"
                   android:layerType="software"
                   android:padding="10dp"
    tools:context="com.example.android.vikramadityastimeline.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/card_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    />



 </FrameLayout>

2
现在它是什么样子?你能分享一下截图吗? - user2756345
你应该为FrameLayout设置padding... - MohammadReza Eram
尝试过了,但没有成功。 - Coolio Cena
我遇到了与此相关的问题。如果我在onBindViewHolder()内更改cardView的背景,那么它的阴影就不会显示。 - MSS
将以下代码添加到你的CardView xml布局文件中:app:cardElevation="2dp"。你可以把2dp修改为任何你想要的数值。 - Aishwarya Tiwari
6个回答

16
为了支持它,您需要在支持 CardView 中指定 app:cardUseCompatPadding="true"
<android.support.v7.widget.CardView
        app:cardElevation="4dp"
        app:cardUseCompatPadding="true"
        app:cardMaxElevation="6dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</android.support.v7.widget.CardView>

app:cardUseCompatPadding="true",这行代码解决了我的问题。 - Umasankar

4
在你的卡片视图中使用这个。它解决了我相同的问题。
app:cardUseCompatPadding="true"

2

在您的代码中,您在recycler视图项中使用了frame layout,并在其中使用了cardview。 您将边距设置为10dp以使frame layout有空白,但是我的建议是删除该行并将边距添加到卡片视图中。 在recycler视图中,项目之间没有空间,因此您的cardview的阴影可能会被隐藏,因为您的外部frame layout在那里完成,所以请尝试通过更改代码来解决:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
             android:layout_width="match_parent"
             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/apk/res-auto"
             android:padding="10dp">


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    app:cardUseCompatPadding="true"
    app:cardMaxElevation="6dp"
    android:layout_marginBottom="10dp"
    app:cardCornerRadius="3dp"
    app:cardElevation="24dp">

    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            />
    </LinearLayout>


</android.support.v7.widget.CardView> 
</FrameLayout>

尝试过了,仍然不起作用,而且高度属性对于我的应用程序中的任何其他元素也不起作用,例如textView或floating button。但是对于我创建的另一个示例应用程序来测试这一点,高度正常工作。并且xml预览显示已添加高度,但在设备上运行时没有显示。 - Coolio Cena
好的,现在尝试将所有的card_view:...替换为app:...并在代码中检查。在运行时不显示高度时,这将是有帮助的。 - Parth Munjpara

0
将以下代码添加到您的 CardView 的 XML 标签中:
android:layout_margin="10dp"

从您的XML中删除以下两行并运行... card_view:cardUseCompatPadding="true" app:cardMaxElevation="6dp" - Vishal Vaishnav
仍然没有效果。 - Coolio Cena
从您的RecyclerView列表项中删除FrameLayout。 - Vishal Vaishnav

0

我也遇到了同样的问题,我找到的解决方案是在AndroidManifest中添加下一个标签:

android:hardwareAccelerated="true"

例子:

<application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/AppTheme">

文档:

https://developer.android.com/guide/topics/graphics/hardware-accel


0

以下是屏幕截图的 XML 示例代码:

<androidx.cardview.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    app:contentPadding="10dp"
    app:contentPaddingBottom="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/name_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name" />
        <TextView
            android:id="@+id/sim_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SIM" />
        <TextView
            android:id="@+id/ID_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ID" />
        <TextView
            android:id="@+id/model_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="model" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

cardView list with shadow


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接