android - 为 DividerItemDecoration 设置可绘制对象

标签 android android-recyclerview

我正在尝试为 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>

最佳答案

将形状改为矩形。

例如:

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

关于android - 为 DividerItemDecoration 设置可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40528012/

相关文章:

java - 用于 ServiceConnection 回调的线程 (Android)

android - 描述、Android、RSS、XML 解析器中的进程标签

android - 从另一个类调用 RecyclerView.Adapter 上的 notifyItemChanged()

java - gradle 类转换异常 : cannot cast to 'java.lang.iterable'

android - 在 Android WebView 中绕过 X-Frame-Options DENY

Android - 每次旋转设备时,RecyclerView 都会倍增

android - ConstraintLayout 中的 RecyclerView 显示不正确

Android:调用 onLongClick() 时移动 FloatingActionButton

Android 模拟器 PlatformException(load_failed_ad,无法重新加载失败的广告,id=571273309,null)

android - RecyclerView 比 Android 中的 viewPager 好吗?