Separator (divider) after last item of ListView

无人久伴 提交于 2019-12-20 08:33:24

问题


When I create a simple layout with only a ListView in it, there is no separator displayed after the last item, which looks a bit ugly.

<?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>

However, I found out that a separator is displayed after the last item if I add another view bellow the listview and set the android:layout_above attribute for the 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_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>

Why does the listview behave like this? How can I get a separator after the last item in a layout that contains only a listview?


回答1:


The answer is very simple: you should change android:layout_height="wrap_content" to android:layout_height="match_parent" in your ListView.

You can probably guess why this happens.




回答2:


Have you tried this one ?

android:footerDividersEnabled="true"

if not try this out

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



回答3:


Add an empty view to top (and/or bottom) to create a divider on top (or bottom)

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



回答4:


I've come with a hack that works around this problem. Since the last separator is displayed only if another view follows the list view, it is possible to make that second view invisible by setting its layout_height to 0dp.

It's still a hack, but it makes to last divider look consistent with the other, so it's better that trying to manually create a horizontal line with trying to guess the right colour and dimensions.

<?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>



回答5:


I do something similar to what @Natix describes, but instead of messing with the containing layout of the list I simply add the empty view as a footer on the list via ListView.addFooterView()




回答6:


It seems somewhat different when your listview is displayed if you code it like that. But even you can follow this too.

First define a style named 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>

// In the above style you can change the height of the line as per your requirement.

Wherever you want to use the above line, you can declare it like this in your xml file.

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

The above code creates a line under your listview. The above code is most useful when you wanna use it in various places in your project. Or if want at only one place, you can do it like this.

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

Hope this helps. BTW The reason is vague and even once I did search for this and I followed this way which I explained above.



来源:https://stackoverflow.com/questions/14199274/separator-divider-after-last-item-of-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!