为DividerItemDecoration设置可绘制对象

71

我试图为DividerItemDecoration设置自定义drawable(线条),但是没有成功。哪里出错了?

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
                LinearLayoutManager.VERTICAL);
dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.sk_line_divider));

XML形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="#000000">
    </stroke>
</shape>

15
顺便说一下,您不需要使用“DividerItemDecoration#setDrawable”,而是可以在主题中设置“android:listDivider”属性。 - arekolek
6个回答

102

将形状改为矩形。

例如:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <size
        android:width="1dp"
        android:height="1dp" />
    <solid android:color="@color/primary" />
</shape>

1
工作得非常出色。 - A P

11

编程方案:

如果您只想为分割线更改颜色而不是创建自定义可绘制对象,可以使用 ColorDrawable:

DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(new ColorDrawable(R.color.greycc));
recyclerView.addItemDecoration(itemDecoration);

如果除了颜色之外,大小也很重要,您可以使用GradientDrawable:

DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);

GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{0xfff7f7f7, 0xfff7f7f7});
drawable.setSize(1,1);
itemDecoration.setDrawable(drawable);

recyclerView.addItemDecoration(itemDecoration);

请注意,将颜色值设置在数组中需要一个完整的十六进制数字字节,否则就会显示错误的颜色,例如,0xFF3E3E3E而不是0X3E3E3E。 彩色分隔符更新 (2023):
val itemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
itemDecoration.setDrawable(
ResourcesCompat.getDrawable(resources, android.R.color.holo_green_light, theme)!!
)
recyclerView.addItemDecoration(itemDecoration)

3
设置ColorDrawable没有效果 - 导致没有任何修饰被绘制出来。 - Roman Kotenko
@RomanKotenko 请查看我的彩色分隔线更新。 - 6rchid

8

如果您想更改分隔线的颜色,可以通过在AppTheme中添加以下行来更改它:
<item name="android:listDivider">@color/your_color</item>

注:您需要将 "your_color" 替换为您想要使用的实际颜色。


2
感谢arekolek,我不知道有这样一个API可以用于android:listDivider
但是,你可以通过几种方式自定义DividerItemDecoration
你需要一个可定制的分隔符drawable。

bg_border_grey.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="1dp"
        android:height="1dp" />
    <solid android:color="@color/borderGrey" />
</shape>

方法一 - 主题

如果您想要应用主题到整个页面,只需添加一个属性即可。然后,它将向下传播到具有相同主题的子元素。

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:listDivider">@drawable/bg_border_grey</item>
</style>

方法二 - 编程方式

如果您只想直接自定义 DividerItemDecoration,只需使用 setDrawable 方法即可。

val dividerItemDecoration = DividerItemDecoration(
    binding.recyclerView.context,
    DividerItemDecoration.VERTICAL
)
dividerItemDecoration.setDrawable(context?.getDrawable(R.drawable.bg_border_grey))
binding.recyclerView.addItemDecoration(dividerItemDecoration)

1
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
                LinearLayoutManager.HORIZONTAL);
dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.line_decoration));
        recyclerView.addItemDecoration(dividerItemDecoration);
        DividerItemDecoration dividerItemDecorationVertical = new DividerItemDecoration(getContext(),
                LinearLayoutManager.VERTICAL);

        dividerItemDecorationVertical.setDrawable(getContext().getResources().getDrawable(R.drawable.line_decoration));
        recyclerView.addItemDecoration(dividerItemDecorationVertical);

0

希望对某些人有所帮助。

基于@Morgan-Koh,以编程方式创建ShapeDrawable

    val decoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    //decoration.setDrawable(ColorDrawable(Color.WHITE))
    val shapeDrawable = ShapeDrawable()
    shapeDrawable.paint.color = Color.WHITE
    shapeDrawable.intrinsicHeight = 2
    shapeDrawable.intrinsicWidth = resources.displayMetrics.widthPixels / resources.displayMetrics.densityDpi
    decoration.setDrawable(shapeDrawable)
    recyclerView?.addItemDecoration(decoration)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接