【问题标题】:Unable to remove extra space between CardView items - Scrollable RecyclerView无法删除 CardView 项目之间的额外空间 - 可滚动的 RecyclerView
【发布时间】:2016-03-13 17:13:15
【问题描述】:

伙计们,

在我的 recyclerview 中尝试删除 cardview 项目之间的额外空间时,我没有选择以下是当前输出。请提出建议。

<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>

【问题讨论】:

  • stackoverflow.com/questions/34240078/modify-cardview-padding 的欺骗?不幸的是,那个人也没有答案。
  • 是的,但是填充比较好,我得到一个难看的卡片视图,。至少需要一个紧凑的视图。
  • 你能张贴你想要的截图吗? @xtechkid
  • 你正在使用很多填充,删除它!
  • card_view:cardUseCompatPadding="true" 在第一个 xml 文件中将其设为“假”

标签: android android-studio android-recyclerview android-cardview


【解决方案1】:

你能试试这个吗?

<?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>

我已经检查过了,它对我来说很好。

查看截图:

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    我还没有实现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>
    

    玩弄填充,你会得到更好的结果。您也可以尝试为您的卡片添加背景颜色,然后您就可以摆脱 RelativeLayout 中的布局边距!

    【讨论】:

    • 让我检查并返回
    • 很高兴能帮上忙 :)
    • 我也做了同样的事情,但它给了我 id 的重复错误 bocz
    • 除非你想要固定卡片(本例中为 2 张),否则在卡片根 LinearLayout 上设置 android:layout_height="wrap_content"
    【解决方案3】:

    将您的卡片布局更改为

    <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">
    

    【讨论】:

      【解决方案4】:

      您需要做的就是从CardLayout 的xml 中删除以下两行:

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

      它的作用是设置两个布局之间的分隔,在这种情况下,您有4dip(顶部有 2 个,底部有 2 个)。您也可以尝试将其缩减为1 dip,并验证它给您带来的效果。

      希望这会有所帮助:)

      【讨论】:

        【解决方案5】:

        在卡片视图 xml 中更改整体父布局宽度以包裹内容。

        android:layout_width="match_parent"
        

        android:layout_width="wrap_content"    
        

        【讨论】: