卡视图布局和分隔线颜色的突出

prominence of card view layout and divider color

我在 list_item.xml "the xml post cell" 中将 CardView 布局用于 RecyclerView",我试图使卡片突出并且分隔线的颜色接近灰色

但是好像没有出现,也没有颜色,
当前情况下的这张图片

这个post_item.xml

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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:layout_margin="5dp"
    android:elevation="6dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:id="@+id/postTitle"
                style="@style/Base.TextAppearance.AppCompat.Title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="This is the post title,
you can check bla bla bla bla bla lkjdflkjhnfldkjhnflkjhnlfkjnglkjnlkjkln" />

            <TextView
                android:id="@+id/postDescription"
                style="@style/Base.TextAppearance.AppCompat.Body2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:ellipsize="end"
                android:maxLines="4"
                android:text="This is the post titldfkjfdjffkjdhfnkjdsfjnkdsjnksskskjfnksjfnksjfnksjfnkjdfbdnfbjvb jkxzbvkjxcvbvkjve,
you can check bla bla bla bla bla lkjdflkjhnfldkjhnflkjhnlfkjnglkjnlkjkln" />

        </LinearLayout>

        <ImageView
            android:id="@+id/postImage"
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="3"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher" />


    </LinearLayout>

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

我试图通过像这样在 RecyclerView 上添加这两行来解决问题 answer 但它没有用
android:divider="#E6E6E6" android:dividerHeight="0px"

经过一些搜索后,我发现 关于同一问题

The problem because this option was false in manifest file

<application android:hardwareAccelerated="true" ...>

还有这个包含一些对我很有帮助的选择像这样

app:cardUseCompatPadding="true"

试试这个代码:

<android.support.v7.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:layout_margin="5dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="6dp"
    app:cardCornerRadius="3dp"
    >

您必须使用:

app:cardElevation="6dp"

而不是:

android:elevation="6dp"

要绘制阴影,您必须使用硬件加速绘图

<application android:hardwareAccelerated="true" ...>

来自 Android 文档的解释:

CardView adds additional padding to draw shadows on platforms before L.

This may cause Cards to have different sizes between L and before L. If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. As an alternative, you can set cardUseCompatPadding flag to true and CardView will add the same padding values on platforms L and after.

Since setting cardUseCompatPadding flag to true adds unnecessary gaps in the UI, default value is false.

如果问题仍然存在,请使用 RecycleView 中的背景颜色。

设置此选项后

app:cardUseCompatPadding="true"
app:cardElevation="6dp"

我建议在 RecyclerView 中添加 off white 颜色

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F5F5F5" // >>> this the color you need
        >

    </androidx.recyclerview.widget.RecyclerView>