效果圖:
預設分割線: 自定義分割線:
看代碼:
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);