【问题标题】:Separator (divider) after last item of ListViewListView 最后一项之后的分隔符(分隔符)
【发布时间】:2012-12-21 09:05:05
【问题描述】:

当我创建一个只有一个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>

但是,我发现如果我在列表视图下方添加另一个视图并为列表视图设置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>

为什么列表视图会这样?如何在仅包含列表视图的布局中的最后一项之后获得分隔符?

【问题讨论】:

  • 如何在仅包含列表视图的布局中的最后一项之后获得分隔符? - 如果布局中只有 ListView,我认为您可以总是(而且很容易)实现一个适配器,它添加一个额外的空行来强制显示目标分隔符。
  • 这很有趣,但最后一项不是可点击的吗?
  • 如果您覆盖适配器的 isEnabled(position) 方法以返回最后一个空位置的 false,则不会。
  • 将 ListView 高度 'from wrap_content' 更改为 'fill_parent'......它可能对我有用。
  • @ASP 比 Kzinch 早了近一个月就给出了正确答案!

标签: android android-listview


【解决方案1】:

答案很简单:你应该把ListView中的android:layout_height="wrap_content"改成android:layout_height="match_parent"

您可能会猜到为什么会发生这种情况。

【讨论】:

  • 如果 ListView 下有内容怎么办?就我而言,我正在制作一个包含几个部分的导航抽屉。
  • 你救了我。我从来不知道这件事。谢谢@Kzinch
  • @Vitali Olshevski:你能解释一下为什么会这样吗?谢谢你的回答...
  • @Kushal 你看,当 ListView 中最后一项的下限与 ListView 本身的下限匹配时,不绘制分隔线。当您使用wrap_content 时,就会发生这种情况。但是当您使用match_parent 时,ListView 的下限与列表中最后一项的下限不同。它们之间有一些额外的空白空间,在最后一项之后由附加的分隔线在视觉上分隔。这是对行为的最简单解释。我希望这会有所帮助。
  • 它也适用于wrap_content,使用空的自定义页脚myList.addFooterView(new View(context)),如下所述
【解决方案2】:

你试过这个吗?

android:footerDividersEnabled="true"

如果不试试这个

<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" />

【讨论】:

  • 这并没有真正的帮助。根据developer.android.com/reference/android/widget/…默认值为true。
  • 嗯,这可能有效,但正如我已经评论过 Appu 的回答,这种颜色和尺寸猜测只是一种技巧,看起来与其他分隔符不一致。如果我只是比较你和 Appu 的值,线条的颜色和高度是非常不同的(并且可能没有一个与默认分隔符的实际颜色和高度相同)。
  • 尝试匹配几乎符合要求的颜色和高度,然后作为解决方法!
  • 但是如果值依赖于给定设备的主题呢?有 Holo 主题、Android 2.x 主题和各种供应商特定的 GUI。我真的不喜欢硬编码一些随机猜测的值,抱歉。
  • 感谢您提出这个问题:android:footerDividersEnabled
【解决方案3】:

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

myList.addHeaderView(new View(context));
myList.addFooterView(new View(context));

【讨论】:

  • 这对我来说效果最好,因为我将样式选项应用于列表视图,我不必担心匹配
【解决方案4】:

我提供了一个可以解决这个问题的 hack。由于仅当列表视图后面有另一个视图时才会显示最后一个分隔符,因此可以通过将第二个视图的 layout_height 设置为 0dp 来使第二个视图不可见。

这仍然是一个 hack,但它使最后一个分隔线看起来与另一个一致,因此最好尝试手动创建一条水平线并尝试猜测正确的颜色和尺寸。

<?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="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/invisible"
        android:layout_alignParentTop="true" />

    <View
        android:id="@+id/invisible"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true" />    
</RelativeLayout>

【讨论】:

  • 如果您在列表视图的底部添加填充,那也可以。我用过:android:paddingBottom="10dp"
【解决方案5】:

我做了类似于@Natix 描述的事情,但我没有弄乱列表的包含布局,而是通过 ListView.addFooterView() 将空视图作为页脚添加到列表中

【讨论】:

    【解决方案6】:

    如果你这样编码,当你的列表视图显示出来时,它似乎有些不同。但即使你也可以遵循这一点。

    首先定义一个名为Line的样式

    <style name="Line">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">2px</item>
        <item name="android:background">@color/white</item>
    </style>
    

    // 在上述样式中,您可以根据需要更改线条的高度。

    无论你想在哪里使用上面的行,你都可以在你的 xml 文件中这样声明它。

    <LinearLayout
    android:orientation = "vertical"
    .......................
    ............................>
     <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             />
    
    
    <View
            style="@style/Line"/>
    
    </LinearLayout>
    

    上面的代码在您的列表视图下创建了一行。当您想在项目的各个地方使用上面的代码时,上面的代码最有用。或者如果只想要一个地方,你可以这样做。

     <LinearLayout
        android:orientation = "vertical"
        .......................
        ............................>
         <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                 />
    <View
        android:layout_width="fill_parent"
            android:layout_height="1px"
            android:background="#20b2aa" />
    
    </LinearLayout>
    

    希望这会有所帮助。顺便说一句,原因很模糊,即使我确实搜索过这个,我也按照上面解释的方式进行了操作。

    【讨论】:

    • 感谢您的努力,但这实际上只是一个 hack。我不想猜测颜色、宽度等。它们需要与列表视图中的其他分隔符相同 - 它们的外观可能取决于应用程序或整个设备的主题。硬编码一些随机猜测的值是一种通向地狱的方式。必须有一些更惯用的解决方案,因为最后一个分隔符确实自动出现在第二种情况下。
    • 正如我在回答中已经提到的,原因未知,我确实搜索了很多次。让我们希望有人能澄清这一点并给出明确的解决方案。
    猜你喜欢
    • 2012-08-01
    • 2017-06-26
    • 2018-02-23
    • 2015-07-17
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多