ListView的最后一项之后的分隔符(分隔符)

Nat*_*tix 51 android android-listview

当我创建一个只有ListView的简单布局时,在最后一项之后没有显示分隔符,这看起来有点难看.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是,如果我在列表视图下添加另一个视图并设置listview的属性,我发现在最后一个项目后面会显示一个分隔符android:layout_above.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_blue_dark"
        android:text="Bottom" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

为什么listview的行为如此?如何在仅包含列表视图的布局中的最后一项之后获取分隔符?

Vit*_*ski 99

答案很简单:你应该改变android:layout_height="wrap_content"android:layout_height="match_parent"你的ListView.

你可能会猜到为什么会这样.

  • 如果您在ListView下有内容怎么办?就我而言,我正在制作一个带有几个部分的导航抽屉. (11认同)
  • @Kushal您会看到,当ListView中最后一项的下限与ListView本身的下限匹配时,不会绘制分隔线.当您使用`wrap_content`时会发生这种情况.但是当你使用`match_parent`时,ListView的下限与列表中最后一项的下限不同.它们之间有一些额外的空白空间,在最后一个项目之后通过附加分隔线在视觉上分开.这是对行为的最简单的解释.我希望这有帮助. (8认同)
  • @Vitali Olshevski:你能解释一下为什么会这样吗?谢谢您的回答... (2认同)

Gri*_*ail 15

你试过这个吗?

android:footerDividersEnabled="true"
Run Code Online (Sandbox Code Playgroud)

如果不试试这个

<View
android:background="#00ff00"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/YOUR_LIST_ID" />
Run Code Online (Sandbox Code Playgroud)

  • 这并没有真正的帮助。根据 http://developer.android.com/reference/android/widget/ListView.html#attr_android:footerDividersEnabled 默认值为 true。 (3认同)

Ste*_*n L 8

在顶部(和/或底部)添加一个空视图以在顶部(或底部)创建分隔线

myList.addHeaderView(new View(context));
myList.addFooterView(new View(context));
Run Code Online (Sandbox Code Playgroud)