欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Recylerview添加分割线与自定义分割线

程序员文章站 2022-03-11 18:44:35
...

效果图:

默认分割线:                                    自定义分割线: 

Recylerview添加分割线与自定义分割线Recylerview添加分割线与自定义分割线

看代码:

1. 添加默认分割线:

mRecylerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

2. 自定义分割线(分两步,①创建drawable文件 ②为recylerview添加drawable分割线)

①创建drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:startColor="@android:color/holo_red_dark"
              android:centerColor="@android:color/white"
              android:endColor="@android:color/holo_blue_light"/>
    <size android:height="2dp"/>
</shape>   

②创建drawable对象,给recylerview添加

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.recylerview_divider, null));
mRecylerView.addItemDecoration(dividerItemDecoration);