Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以使用布局而不是可绘制来进行分割?_Android_Android Layout_Kotlin - Fatal编程技术网

Android 是否可以使用布局而不是可绘制来进行分割?

Android 是否可以使用布局而不是可绘制来进行分割?,android,android-layout,kotlin,Android,Android Layout,Kotlin,我想在RecyclerView中的水平滚动项目之间使用自定义分隔符。我可以使用图标作为Drawable,但是这个图标作为分隔符填充了整个空间,并且看起来被拉伸了。我制作了自定义的RelativeLayout,将此分隔符居中放置为ImageView,并向其添加了自定义填充。我不想将此分隔符(箭头)添加到布局的垂直中心,但略低于中心。它是带有自定义填充的自定义图标 也就是说,我想以某种方式将此分隔符添加到DividerItemDecoration中,但它只接受可绘制的 更新: 我试图膨胀这个自定义布

我想在
RecyclerView
中的水平滚动项目之间使用自定义分隔符。我可以使用图标作为
Drawable
,但是这个图标作为分隔符填充了整个空间,并且看起来被拉伸了。我制作了自定义的
RelativeLayout
,将此分隔符居中放置为
ImageView
,并向其添加了自定义填充。我不想将此分隔符(箭头)添加到
布局的垂直中心,但略低于中心。它是带有自定义填充的自定义图标

也就是说,我想以某种方式将此分隔符添加到
DividerItemDecoration
中,但它只接受
可绘制的

更新:

我试图膨胀这个自定义布局,并将其转换为可绘制。 问题是充气布局的宽度和高度为0

val view = LayoutInflater.from(app).inflate(R.layout.separator, null)
val bitmapDecoration = view?.let { viewToBitmap(it, it.width, it.height) }
val drawableDecoration = BitmapDrawable(app.resources, bitmapDecoration)
itemDecoration.setDrawable(drawableDecoration)
我的分隔符示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="40dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:paddingTop="4dp"
        android:src="@drawable/arrow_black_short"/>

</RelativeLayout>

图像(recyclerview项目之间的自定义分隔符):

如果我只使用分隔符图标作为
Drawable
,并将其添加到
DividerItemDecoration
中,它看起来是这样的(它将拉伸Drawable以填充父对象):


您需要创建一个绘制分割项装饰的类,而不是使用视图或布局。详细介绍了为什么要走这条路线,并提供了一个示例

从api 25开始,您也可以使用此选项

var decoration: DividerItemDecoration =  DividerItemDecoration(getApplicationContext(), VERTICAL)
recyclerview.addItemDecoration(decoration)
我希望这有助于您正确了解自己的目标。

更新:

  • 创建以下类:

    public class HorizontalDrawableDecoration extends RecyclerView.ItemDecoration {
        private final int paddingTopPx, paddingBottomPx, paddingLeftPx, paddingRightPx;
        private final Drawable drawable;
        private Rect bounds;
    
        public HorizontalDrawableDecoration(Context context,
                                        @DrawableRes int drawableId,
                                        @DimenRes int paddingTop, @DimenRes int paddingBottom,
                                        @DimenRes int paddingLeft, @DimenRes int paddingRight ) {
            this.paddingTopPx = getDimenPx(context, paddingTop);
            this.paddingBottomPx = getDimenPx(context, paddingBottom);
            this.paddingLeftPx = getDimenPx(context, paddingLeft);
            this.paddingRightPx = getDimenPx(context, paddingRight);
            this.drawable = ContextCompat.getDrawable(context, drawableId);
            bounds = new Rect();
        }
    
        @Override
        public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.onDraw(canvas, parent, state);
            int top = Math.round((parent.getHeight() + paddingTopPx + paddingBottomPx - drawable.getIntrinsicHeight()) / 2f);
            int bottom = top + drawable.getIntrinsicHeight();
            int childCount = parent.getChildCount();
            for (int i=0; i< childCount; i++) {
                View child = parent.getChildAt(i);
                parent.getDecoratedBoundsWithMargins(child, bounds);
                final int right = bounds.right + Math.round(child.getTranslationX()) - paddingRightPx;
                final int left = right - drawable.getIntrinsicWidth();
                drawable.setBounds(left, top, right, bottom );
                drawable.draw(canvas);
            }
        }
    
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if (parent.getChildAdapterPosition(view) == state.getItemCount() - 1) {
                outRect.setEmpty();
            } else {
                outRect.set(0,0, drawable.getIntrinsicWidth() + paddingLeftPx + paddingRightPx, 0);
            }
        }
    
        private int getDimenPx(Context context, @DimenRes int dimenId) {
            return (context == null || dimenId == 0)? 0: context.getResources().getDimensionPixelSize(dimenId);
        }
    }
    
  • 还有一件事,将dimens添加到res/values中的dimens.xml文件中:

    <dimen name="dp8">8dp</dimen>
    <dimen name="dp4">4dp</dimen>
    
    8dp
    4dp
    

  • 你可以发布你实际需要的图片吗?你可以创建自己的类继承自
    RecyclerView.ItemDecoration()
    我创建了自己的类,但我不知道如何使用Layout而不是Drawable,因为每个方法都使用Rect。这很有效,但我现在无法设置自定义填充。由于某些原因,它在GetItemOffset中被忽略。我想在那里设置填充开始和结束。你想在哪里填充,箭头?是的,箭头分隔符的左右两侧应该有填充8dp。
    <dimen name="dp8">8dp</dimen>
    <dimen name="dp4">4dp</dimen>