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

本文介绍了如何在 Android 开发中使用 RecyclerView 添加默认及自定义分割线。通过代码示例展示了两种分割线的实现方法,包括使用系统提供的 DividerItemDecoration 类以及自定义 drawable 资源文件来实现更灵活的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

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

看代码:

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

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值