无法在水平RecycleView中的项目之间添加边距

时间:2017-09-26 06:20:57

标签: android android-recyclerview

我使用水平RecyclerView,我的列表项之间的边距是常量,我想增加列表项间隙,我尝试增加layout_margin但是没有变化。

下面是我的列表项目布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:background="@color/colorWhite"
app:cardElevation="2dp"
app:cardCornerRadius="5dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackground"
    android:orientation="vertical"
    android:padding="0dp">

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher_background" />


    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/itemImage"
        android:gravity="center"
        android:padding="5dp"
        android:text="Sample title"
        android:textColor="@color/colorTextBlack"
        android:textSize="16dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="14dp"
        android:gravity="center"
        android:textSize="12dp"
        android:textColor="#9f9f9f"
        android:text="2 Aug, London" />

</LinearLayout>

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

这是我得到的观点,我想增加项目之间的差距 enter image description here

有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

您可以像这样更改XML,并可以尝试

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

      <android.support.v7.widget.CardView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_margin="10dp"
           android:background="@color/colorAccent"
           app:cardCornerRadius="5dp"
           app:cardElevation="2dp"
           app:cardPreventCornerOverlap="false"
           app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:selectableItemBackground"
        android:orientation="vertical"
        android:padding="0dp">

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="170dp"
            android:layout_height="170dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />


        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/itemImage"
            android:gravity="center"
            android:padding="5dp"
            android:text="Sample title"
            android:textColor="@color/cardview_dark_background"
            android:textSize="16dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="14dp"
            android:gravity="center"
            android:text="2 Aug, London"
            android:textColor="#9f9f9f"
            android:textSize="12dp" />

    </LinearLayout>

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

答案 1 :(得分:0)

试试这个

recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
            DividerItemDecoration.VERTICAL));

为了自定义它,你可以添加一个自定义drawable:

DividerItemDecoration itemDecorator = new DividerItemDecoration(new 
DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
itemDecorator.setDrawable(ContextCompat.getDrawable(getActivity(), 
R.drawable.divider));

divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<size android:height="0.5dp"/> //change height acc to your need.

答案 2 :(得分:0)

您可以简单地在列表项布局中为Cardview添加一些填充。

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="30dp"
    android:background="@color/colorWhite"
    android:paddingStart="10dp"
    android:paddingEnd="10dp"
    app:cardElevation="2dp"
    app:cardCornerRadius="5dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">
相关问题