2017-04-04 131 views
0

想要在不使用cardUseCompatPadding的情况下在CardView卡之间添加间距,我如何实现这一点?想要在Android中CardView卡之间添加水平间距?

+0

You ca n使用_padding_。更多细节显示截图。 – Piyush

+0

为什么你不想使用cardUseCompatPadding ?????? – AwaisMajeed

+0

使用cardUseCompatPadding如果我正在左/右背景不需要填充,它带有更多的高度和宽度 – Divya

回答

1

的行集填充,使所有项目的每个项目

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

// here your views 

</LinearLayout> 

,或者使用类似这样通过Java来saperate项目

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(mLayoutManager); 
    recyclerView.setNestedScrollingEnabled(false); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing); 
    recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels)); 

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { 
private int space; 

public SpacesItemDecoration(int space) { 
    this.space = space; 
} 

@Override 
public void getItemOffsets(Rect outRect, View view, 
          RecyclerView parent, RecyclerView.State state) { 
    outRect.left = space; 
    outRect.right = space; 
    outRect.bottom = space; 

    // Add top margin only for the first item to avoid double space between items 
    if (parent.getChildLayoutPosition(view) == 0) { 
     outRect.top = space; 
    } else { 
     outRect.top = 0; 
    } 
} 

之间设置间距}

+1

谢谢! java代码为我工作 – Divya

+1

标记为正确我的答案@Divya –