如何在RecyclerView中的CardView之间设置相等的空间?

时间:2018-07-31 07:38:06

标签: android android-recyclerview cardview

我的 RecyclerView中有一些 CardViews ,当我想通过增加边距在它们之间设置空间时,会得到以下提示:

enter image description here

如您所见,卡之间的距离是原来的两倍。例如,如果我仅在顶部,左侧和右侧设置边距,问题就解决了,但是最后一张卡会触碰屏幕的底部,我不希望这样。像这样:

enter image description here

如您所见,所有空间都是平等且良好的,但底部没有空间。 我的项目中有MainActivity.java,activity_main.xml,recyclerview_row.xml和MyAdapter.java。

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:background="#dddddd">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

这是我的recyclerview_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true"
    app:cardPreventCornerOverlap="false"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:id="@+id/image_view"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_view"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"/>

    </LinearLayout>

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

我的问题是如何在这些卡之间设置相等的距离。谢谢。

4 个答案:

答案 0 :(得分:2)

如下更改您的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:background="#dddddd">

<android.support.v7.widget.RecyclerView
    android:paddingBottom="10dp"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>

魔术是通过以下这一行完成的:android:clipToPadding="false"

答案 1 :(得分:1)

在“主要活动”中尝试一下,

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:background="#dddddd">

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

答案 2 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:background="#dddddd">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

答案 3 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".MainActivity"
        android:paddingBottom="@dimen/dim_10"
        android:background="#dddddd">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view">
        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>