Add custom dividing line RecyclerView

The default light gray dividing line at some point and can not meet our requirements, then you need to customize a dividing line up.

You need to call the method and pass a Drawable function object on it.setDrawable(@NonNull Drawable drawable)

Now you can use to write a dividing line shape style:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:centerColor="#ff00ff00" //绿色
        android:endColor="#ff0000ff"    //蓝色
        android:startColor="#ffff0000"  //红色
        android:type="linear" />
    <size android:height="3dp" />

</shape>

Add code division line to read as follows:

//添加自定义分割线
DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.custom_divider));
recyclerView.addItemDecoration(divider);

After running, you can see a colorful line of division:

 

Guess you like

Origin www.cnblogs.com/renhui/p/11465200.html