android动态添加view设置间距,想要在Android中CardView卡之间添加水平间距?

这篇博客介绍了如何在Android中使用RecyclerView创建一个垂直布局,并设置了背景颜色、内边距和项目间隔。通过LinearLayoutManager配置布局管理器,禁用嵌套滚动,设置默认动画,并利用SpacesItemDecoration为每个项目添加间距。
摘要由CSDN通过智能技术生成

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

android:orientation="vertical"

android:background="#FFF"

android:padding="5dp"

android:layout_width="match_parent"

android:layout_height="wrap_content">

// here your views

,或者使用类似这样通过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;

}

}

之间设置间距}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值