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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章