RecyclerView 周围的白色边框

White border around RecyclerView

我有一个带有项目的 RecyclerView。当我 运行 它在 Lollipop 设备上时,一切都很棒。但是当我 运行 它在棒棒糖之前的设备上时,每个视图周围都有一个白色边框。

编辑:我之前没有post编码,因为我认为是某种错误。

Rec.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"      
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333333"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/text"
    android:scrollbars="vertical" />

</RelativeLayout>

main_card_view.xml

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#222222"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/image"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />
    <RelativeLayout
        android:layout_width="320dp"
        android:layout_height="240dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignBottom="@+id/image"
        android:background="#bb000000"
        android:id="@+id/rel_color">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Μπύρα"
            android:textColor="#fcfcfc"
            android:textSize="40sp"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowColor="#000000"
            android:id="@+id/text"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:gravity="center" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8$"
            android:id="@+id/textView3"
            android:textColor="#fcfcfc"
            android:layout_alignParentBottom="true"
            android:textSize="28sp"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowColor="#000000"
            android:layout_alignBottom="@+id/text"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_margin="10dp" />
    </RelativeLayout>

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

The problem is because of CardView.

在您的 CardView 上设置 setPreventCornerOverlap(false)

并扩展 ImageView 以创建自定义 class,如 RoundedCornerImageView,并在 CardView 中使用它而不是 ImageView。

Answer on this question will be helpful.

注意: 您可以自定义 class 即 RoundedCornerIm... 或使用链接的 SO 问题中的库,即 here on github

编辑:

好吧,我觉得你有点困惑,所以,

1) RecyclerView 是您的 list/grid 的容器布局。即

android:id="@+id/my_recycler_view"

正如我可以在图片中显示的那样,您必须将 GridLayoutManager 附加到 RecyclerView。同样,要将数据放入 RecyclerView,您还必须附加一个 adapter

同样,如果您有适配器,即扩展适配器 class(可能 RecyclerView.Adapter)的 class。现在必须给适配器 class 一个布局来膨胀,即你的 main_card_view.xmlRecyclerView 中以网格样式因为 GridLayoutManager.

所以您的 RecyclerView 中的每个项目都是 main_card_view.xml

现在在扩展适配器 class 的 Class 中(即附加到回收视图)将有 CardView -- 你必须在那里进行更改。