Android列表ListView的分割线添加边距

ListView列表我们添加分割线一般如下:

<ListView
     android:id="@+id/taskList1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:divider="@color/list_divider"
     android:dividerHeight="1dp"/>

divider和dividerHeight配合使用列表就有分割线了,但问题来了,分割线是通透的,与列表同宽,如果想要添加左右边距怎么办呢?我们需要使用一个xml图片资源。

自己定一个list_divider_bg放到drawable里面

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="14dp"
       android:insetRight="14dp">

    <shape android:shape="rectangle">
        <solid android:color="@color/c_e6"/>
    </shape>

</inset>

然后在ListView中引用即可

<ListView
     android:id="@+id/taskList1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:divider="@drawable/list_divider_bg"
     android:dividerHeight="1dp"/>
发布了59 篇原创文章 · 获赞 88 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/geofferysun/article/details/78072259