如何在RecyclerVIew中删除卡之间的不必要空间?

时间:2018-12-12 12:51:22

标签: android android-recyclerview recycler-adapter

我正在使用Recycler视图+卡视图显示我的内容,但是卡之间没有多余的空间来删除​​它。 我曾尝试用负填充,但没有用。还有其他解决方案吗?或我的代码中有任何错误。

我的RecyclerView

<android.support.v7.widget.RecyclerView
    android:layout_marginStart="4dp"
    android:layout_marginEnd="4dp"
    android:id="@+id/reyclerview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:scrollbars="vertical" />

cardview

<?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"
xmlns:card_view="http://schemas.android.com/tools"
app:cardCornerRadius="4dp"
card_view:cardElevation="0dp"
card_view:cardUseCompatPadding="true"
card_view:cardPreventCornerOverlap="false"
android:background="@color/background">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="4dp">

   //Some Items


</RelativeLayout>

这是空间的预览

enter image description here

3 个答案:

答案 0 :(得分:0)

xml:

card_view:cardPreventCornerOverlap="false"

或JAVA

cardView.setPreventCornerOverlap(false)

ref:Android CardView remove padding

答案 1 :(得分:0)

我通过将cardview放在LinearLayout中解决了它

<?xml version="1.0" encoding="utf-8"?><LinearLayout
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"
xmlns:card_view="http://schemas.android.com/tools">
<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="4dp">

    <ImageView
        android:id="@+id/i1"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/t1"
        android:layout_width="316dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="60dp"
        android:text="Text1"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/t2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/t1"
        android:maxLines="2"
        android:layout_alignStart="@+id/t1"
        android:layout_marginStart="8dp"
        android:layout_marginTop="10dp"
        android:text="Text2"
        android:textSize="12sp" />


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

答案 2 :(得分:-1)

从Cardview中删除此行并尝试

card_view:cardUseCompatPadding="true"
相关问题