gpt4 book ai didi

android - 想在 android 中的 CardView 的卡片之间添加水平间距?

转载 作者:行者123 更新时间:2023-12-05 00:12:46 24 4
gpt4 key购买 nike

想在不使用 cardUseCompatPadding 的情况下在 CardView 的卡片之间添加间距,我该如何实现?

最佳答案

在行上设置填充,以便所有项目设置每个项目之间的间距

<?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 像这样使用来分离项目

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;
}
}

关于android - 想在 android 中的 CardView 的卡片之间添加水平间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com