2017-09-26 66 views
1

我使用水平RecyclerView,我的清单项目之间的保证金是恒定的,我想增加清单项目缺口,我尝试增加layout_margin但没有变化。无法在水平RecycleView项目之间添加保证金

下面是我的列表项布局

<?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之间的差距

有人可以帮助我的问题?

+0

为'CardView'应用'padding_right'。你也可以使用ItemDecorator。 – Piyush

+0

检查这个答案https://stackoverflow.com/a/44543420/7666442 –

+0

@NileshRathod我试过这个答案,仍然为列表中的第一项边距未设置 –

回答

2

你可以像这样改变你的XML nd可以试试,

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

为我工作,谢谢! –

+0

太好了。快乐编码:) – Kuls

0

尝试这样

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

为定制它一点点,你可以添加自定义绘制:

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.