无法移除CardView项之间的额外空格 - 可滚动的RecyclerView

6

大家好,

我正在尝试删除我的recyclerview中卡片视图项之间的额外空格,以下是当前输出。请给予建议。

在此输入图片描述

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#303030"
    android:id="@+id/cv"
    android:layout_marginTop="2dip"
    android:layout_marginBottom="2dip"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="5dp"
    android:foreground="?android:attr/selectableItemBackground"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:padding="5dp"
        android:longClickable="true"
        android:background="#303030">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_photo"
            android:background="@drawable/vector_red"
            android:layout_alignBottom="@+id/txtSub" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/txtMain"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_toEndOf="@+id/person_photo"
            android:elevation="4dp"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/txtSub"
            android:layout_below="@+id/txtMain"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_toEndOf="@+id/person_photo" />

    </RelativeLayout>

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

我的回收站视图

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    card_view:cardUseCompatPadding="false"
    card_view:cardPreventCornerOverlap="false"
    android:background="#0fffffff" />

 </RelativeLayout>

这是一个重复的问题,与http://stackoverflow.com/questions/34240078/modify-cardview-padding相同。不幸的是,那个问题也没有答案。 - alzee
对,但填充相对来说还可以,我得到了一个丑陋的卡片视图,需要至少一个紧凑的视图。 - Sudheej
你能发一下你想要的屏幕截图吗?@xtechkid - Kaushik
你使用了很多填充,只需将其删除即可! - Skynet
在第一个XML文件中,将card_view:cardUseCompatPadding="true"更改为"false"。 - Moinkhan
5个回答

5

我还没有实现一个RecyclerView,但是我已经在线性布局中复制了两行,减去了你原来问题中的填充:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:background="#303030"
        android:id="@+id/cv1"
        card_view:cardElevation="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_margin="4dp"
            android:longClickable="true"
            android:background="#303030">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:background="@drawable/vector_red"
                android:layout_alignBottom="@+id/txtSub" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/txtMain"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo"
                android:elevation="4dp"
                android:textSize="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/txtSub"
                android:layout_below="@+id/txtMain"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:background="#303030"
        android:id="@+id/cv1"
        card_view:cardElevation="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_margin="4dp"
            android:longClickable="true"
            android:background="#303030">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:background="@drawable/vector_red"
                android:layout_alignBottom="@+id/txtSub" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/txtMain"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo"
                android:elevation="4dp"
                android:textSize="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/txtSub"
                android:layout_below="@+id/txtMain"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo" />

        </RelativeLayout>

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

</LinearLayout>

调整内边距会得到更好的结果。你也可以尝试为卡片添加背景色,这样就可以消除相对布局中的布局边距!


让我检查一下,稍后回复。 - Sudheej
很高兴能帮到你,伙计 :) - Skynet
我已经做了同样的事情,但由于重复,它给了我ID错误。 - robinHood013
除非您想要固定的卡片(在此情况下为2),否则请在Card根LinearLayout上设置android:layout_height="wrap_content"。 - Stephane

2
你只需要从 CardLayout 的 xml 中移除以下两行即可:

android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"

这样做的作用是设置两个布局之间的间隔,在这种情况下,您有4dip(上面2个和下面2个)。您还可以尝试将其减少到1 dip并验证它给您的效果。
希望能对您有所帮助 :)

0

你能试试这个吗?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
...
...
...
</android.support.v7.widget.CardView>

我已经检查过了,对我来说运行良好。

请查看截图:

enter image description here

希望这可以帮到你。

0

将您的卡片布局更改为

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:background="#303030"
         android:id="@+id/cv"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="2dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true">

0
在卡片视图的XML中,将整体父布局的宽度更改为wrap content。
android:layout_width="match_parent"

android:layout_width="wrap_content"    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接