我试图为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>
答案 0 :(得分:50)
将形状更改为矩形。
例如:
<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 :(得分: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);
答案 2 :(得分:1)
解决方案(以编程方式):
如果您只想更改分隔线的颜色,而不是创建自定义可绘制对象,则可以使用ColorDrawable:
DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(new ColorDrawable(R.color.greycc));
recyclerView.addItemDecoration(itemDecoration);
如果大小很重要,除了颜色之外,您还可以使用GradientDrawwable:
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);
请注意,在数组中设置颜色值需要十六进制值的完整八位位组,否则将显示不正确的颜色,即0x FF 3E3E3E而不是0X3E3E3E
答案 3 :(得分:1)
如果要更改分隔线颜色,可以通过添加以下行从AppTheme中更改它
<item name="android:listDivider">@color/your_color</item>