android 增加回收器视图项目装饰线的粗细

xn1cxnb4  于 2022-11-20  发布在  Android
关注(0)|答案(4)|浏览(160)

在我的活动中,我使用了网格布局的回收者视图,我只是简单地设置了回收者视图项目装饰,它成功地将线条添加到回收者视图中,但这些线条的厚度太薄,不可见,所以我的问题是如何增加这些线条的厚度并更改其颜色。
下面是我使用的代码:

recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
        recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL));

回收程序视图的屏幕截图:
Screenshot of recyclerview

jhkqcmku

jhkqcmku1#

我想我找到了你的问题的解决方案...你需要实现一个可绘制的文件来增加装饰线的厚度...并将其添加到装饰...要更改颜色,在可绘制的文件中你声明你想在装饰线中显示的颜色...

解决方案如下:

将divider.xml添加到可绘制文件夹中...

分隔符.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
<size
    android:width="1dp"
    android:height="15dp"/>

    <solid android:color="@android:color/white"/>
</shape>

这与你的活动有关...

Drawable mDivider = ContextCompat.getDrawable(this, R.drawable.divider);
 dividerItemDecoration.setDrawable(mDivider);
vmdwslir

vmdwslir2#

以下是更简洁的解决方案:

首先我们可以使用DividerItemDecoration来设置线条。除了设置粗细之外,还可以设置分隔线的颜色

  • GradientDrawable.setSize(width, height)以像素为单位设置尺寸;指定高度将调整分隔条的厚度。
  • GradientDrawable构造函数接受一个渐变方向和一个颜色整数数组;必须指定两种颜色,并且可以将它们保持为相同的值。
DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{0xfff7f7f7, 0xfff7f7f7});
drawable.setSize(1,3);
itemDecoration.setDrawable(drawable);
recyclerView.addItemDecoration(itemDecoration);
gojuced7

gojuced73#

您可以使用此选项设置网格的平均值

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
  private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

/**
 * Converting dp to pixel
 */
private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

然后,

recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
yeotifhr

yeotifhr4#

请尝试以下java代码

ShapeDrawable vLine = new ShapeDrawable(new Shape() {
    @Override
    public void draw(Canvas canvas, Paint paint) {
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLACK);
        canvas.drawLine(0, 0, 0, 500, paint);
    }
});
ShapeDrawable hLine = new ShapeDrawable(new Shape() {
    @Override
    public void draw(Canvas canvas, Paint paint) {
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLACK);
        canvas.drawLine(0, 0, 500, 0, paint);
    }
});
DividerItemDecoration vDID = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
DividerItemDecoration hDID = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
vDID.setDrawable(vLine);
hDID.setDrawable(hLine);
recyclerView.addItemDecoration(vDID);
recyclerView.addItemDecoration(hDID);

注意:您可以通过XML来实现,但是上面的解决方案是一个地方的解决方案,尽管代码看起来很大。

相关问题