android 为DividerItemDecoration设置可绘制对象

sqyvllje  于 2023-03-21  发布在  Android
关注(0)|答案(5)|浏览(126)

我试图为DividerItemDecoration设置自定义可绘制对象(线条),但没有成功。错误在哪里?

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>
nnvyjq4y

nnvyjq4y1#

将形状更改为矩形。
例如:

<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>
j13ufse2

j13ufse22#

编程(解决方案):

如果您只想更改分隔线的颜色而不是创建自定义可绘制对象,则可以使用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)
vq8itlhq

vq8itlhq3#

如果要更改分隔线颜色,可以通过添加以下行从AppTheme进行更改
<item name="android:listDivider">@color/your_color</item>

rryofs0p

rryofs0p4#

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);
v09wglhw

v09wglhw5#

多亏了arekolek,我才知道android:listDivider还有这样的API。
但是,有几种方法可以定制DividerItemDecoration
您需要一个可绘制的定制分隔符。
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>

方法1-主题

如果你想广泛应用主题,你可以只添加一个属性,然后,它将传播到具有相同主题的子节点。
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>

方法#2 -编程

如果你只想直接定制DividerItemDecoration本身,那么只需要setDrawable,你就可以随心所欲了。

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

相关问题