我在list_item.xml
使用了CardView布局“对于RecyclerView的xml post cell”,我试图使卡片突出,并且分隔符的颜色接近灰色,就像这样
但它似乎没有出现或颜色, 这个图像是针对当前的情况
这个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"
如果问题仍然面临问题,请在RecycleView中使用背景颜色。
试试这段代码:
<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"
要绘制阴影,必须使用hardwareAccelerated绘图
<application android:hardwareAccelerated="true" ...>
Android doc的解释:
CardView添加额外的填充以在L之前在平台上绘制阴影。
这可能导致卡片在L和L之间具有不同的大小。如果需要将CardView与其他视图对齐,则可能需要api版本特定的维度资源来考虑更改。作为替代方案,您可以将cardUseCompatPadding标志设置为true,并且CardView将在平台L和之后添加相同的填充值。
由于将cardUseCompatPadding标志设置为true会在UI中添加不必要的间隙,因此默认值为false。
设置此选项后
app:cardUseCompatPadding="true"
app:cardElevation="6dp"
我建议在RecyclerView中添加白色的颜色
<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>