CardView 和 RecyclerView 分隔行为

CardView and RecyclerView divider behaviour

我正在尝试将 RecyclerViewCardView 一起用作列表中的单个项目。
我想实现水平列表,用LayoutManager真的很容易。
所以我开始实施它。似乎一切正常,但与我预期的不同,这里是使用 CardView 作为列表项的结果。

看起来不错,但我没有设置任何填充、分隔线和圆角。
这是我的 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="match_parent">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:gravity="center"
       >
        <ImageView
            android:id="@+id/image_view"
            android:src="@drawable/androidsss"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            />
        <TextView
            android:id="@+id/info_text"
            android:layout_below="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="@android:color/black"
            android:textSize="14sp" />
    </RelativeLayout>

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

布局底部还有类似小分隔线的东西。
我没有指定任何内容。
这是我为 RecyclerView

设置的代码
 mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(mLayoutManager);
 //   mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
    mAdapter = new CardViewDataAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);

有趣的是,对于简单的项目,而不是卡片视图,一切都按预期进行。这是一个例子。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="150dp"
    android:layout_height="100dp"
    android:background="@android:color/white"
    >

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        />
    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/item_image"
        android:text="Hello World !!"
        android:layout_centerInParent="true"
        android:textColor="@android:color/black"
        />
</RelativeLayout>

哪里出了问题,是我的错还是bug?

这是 CardView 的默认行为,但是您可以通过向 CardView 声明添加属性来修改它的外观,例如删除边框半径等。

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="200dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardBackgroundColor='#ffffff'>

有关更多信息,请访问此处以剪辑阴影 https://developer.android.com/training/material/shadows-clipping.html https://developer.android.com/reference/android/support/v7/widget/CardView.html

您可以看到的空间是 compat paddings,它们仅在 Lollipop 之前的设备上可见(如果您设置 card_view:cardUseCompatPadding="true",则在任何地方都可见)。您可以找到值 here(CardView 的文档)。

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.

此外,据我所知,CardView 的角有一些默认半径(可能是 2dp)。

您可以尝试任何技巧来避免这些空格,或者(恕我直言,这更好)考虑是否应该使用自定义背景 视图 ,例如简单的 "tile"。 Here 你从 Google 那里获得了一些关于设计的信息。

试试这个布局

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:gravity="center">

            <ImageView
                android:id="@+id/image_view"
                android:src="@drawable/androidsss"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentTop="true"
                android:layout_centerInParent="true" />

            <TextView
                android:id="@+id/info_text"
                android:layout_below="@+id/image_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="@android:color/black"
                android:textSize="14sp" />
        </RelativeLayout>

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