在我的活动中,我使用的是带有网格布局的回收站视图,我只是在设置回收站视图项装饰,从而成功地将行添加到回收站视图,但是这些行的厚度太细且不可见,所以我的问题是我增加了线条的粗细并更改了颜色。
这是我使用的代码:
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL));
我的回收者视图的屏幕截图:
答案 0 :(得分:0)
您可以使用它来平均设置网格
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));
答案 1 :(得分:0)
我想我找到了您问题的解决方案...您需要实现一个可绘制文件,以增加装饰线条的厚度...并将其添加到装饰中。.要更改颜色,请在可绘制中声明您的颜色,想要在装饰线条中显示。.
这是解决方案:
在可绘制文件夹中添加divider.mxl ...
divider.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<size
android:width="1px"
android:height="15px"/>
<solid android:color="@android:color/white"/>
</shape>
这符合您的活动。
Drawable mDivider = ContextCompat.getDrawable(this, R.drawable.divider);
dividerItemDecoration.setDrawable(mDivider);
答案 2 :(得分:0)
尝试此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来实现,但是上述解决方案是一处解决方案,即使代码看起来很大。
答案 3 :(得分:0)
这是一个更好的解决方案:
首先,我们可以使用 DividerItemDecoration 设置行。除了设置厚度,您还可以设置分隔线的颜色:
GradientDrawable.setSize(width, height)
以像素为单位设置尺寸;指定高度会调整分隔线的厚度。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);