---
title: "ListView设置分割线的长度"
author: "flycatdeng"
date: "2016-01-25T20:53:23Z"
tags: ["listview", "分割线", "长度"]
categories: ["移动开发"]
slug: "listview"
draft: false
lastmod: "2016-01-25T20:53:23Z"
id: 2273790
url: "https://flycatdeng.iteye.com/blog/2273790"
---

android中ListView默认的分割线是填充满整个listview的宽度的，然后设计师需要的偏偏就是不填充满的状态，找了相关资料，写了个demo验证下。

![](http://dl2.iteye.com/upload/attachment/0114/7847/10f2294c-f469-3a47-b8a0-b0eac9bccc07.png) ![](http://dl2.iteye.com/upload/attachment/0114/7845/75bd1c80-23f7-3c9f-af10-73a8bc82cbb7.png)

当然最简单的实现方式就是用图片了，但是图片就要涉及到分辨率，那用颜色填充会比较直接。

在drawable中定义我需要的分割线main\_list\_divider\_line.xml

```
<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="#33000000" />
        </shape>
    </item>
</layer-list>
```

 其中android:left表明其距离左边的距离，android:right表示距离右边的距离，android:shape="rectangle"表示是用一个矩形填充，android:color="#33000000"填充的颜色。

然后在配置listview的地方设置分割线为上述填充物即可

```
    <ListView
        android:id="@+id/main_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/main_list_divider_line"
        android:dividerHeight="1px"
        android:layout_marginTop="14dp" >
    </ListView>
```