Separator (divider) after last item of ListView

前端 未结 6 1970
再見小時候
再見小時候 2021-01-31 01:46

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.



        
相关标签:
6条回答
  • 2021-01-31 02:22

    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>
    
    0 讨论(0)
  • 2021-01-31 02:29

    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));
    
    0 讨论(0)
  • 2021-01-31 02:33

    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.

    0 讨论(0)
  • 2021-01-31 02:33

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

    0 讨论(0)
  • 2021-01-31 02:44

    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" />
    
    0 讨论(0)
  • 2021-01-31 02:44

    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.

    0 讨论(0)
提交回复
热议问题