顶部和底部分隔符未显示在Android listview中

顶部和底部分隔符未显示在Android listview中,android,listview,divider,Android,Listview,Divider,按照我的理解,为listview定义的分隔符也应该出现在列表的顶部和底部,即第一项的上方和最后一项的下方 由于某些原因,它们不会出现在我的listview中: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_wid

按照我的理解,为listview定义的分隔符也应该出现在列表的顶部和底部,即第一项的上方和最后一项的下方

由于某些原因,它们不会出现在我的listview中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background">

<ImageView
    android:id="@+id/home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="3dp"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:src="@drawable/homeicon"/>

<TextView
    android:id="@+id/titleBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Page Title" 
    android:layout_marginLeft="10dp" 
    android:paddingTop="3dp"
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:typeface="serif" 
    android:textColor="#FFFFFF"
    android:layout_centerHorizontal="true"/>

<ImageView
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:src="@drawable/backicon" 
    android:layout_alignParentRight="true"/>


<ImageView
    android:id="@+id/separator1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/home"
    android:scaleType="fitXY"
    android:src="@drawable/separatorimg" />


<ListView
    android:id="@android:id/android:list" 
    android:layout_below="@+id/separator1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separatorimg">
</ListView>

</RelativeLayout>

将android:footerDividersEnabled设置为true(这是关于这个问题的其他问题中给出的答案)不会改变任何事情。此外,我没有禁用的项目,除非有一个默认的页脚将被禁用

有什么想法吗


编辑:当我滚动过第一个/最后一个项目时,顶部和底部分隔符处的分隔符似乎会在一瞬间出现,然后消失。界面似乎将继续,直到后来才意识到这是最后一项,然后禁用分隔符。

我认为您必须使用listview下面的视图设置页脚。我也有同样的问题,这是我可以得到的解决方案:


请在下面找到修改后的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >

<LinearLayout
    android:id="@+id/llAppBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:weightSum="10" >

    <ImageView
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingBottom="3dp"
        android:paddingLeft="3dp"
        android:paddingTop="3dp"
        android:scaleType="fitXY"
        android:src="@drawable/homeicon" />

    <TextView
        android:id="@+id/titleBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="6"
        android:paddingTop="3dp"
        android:text="Page Title"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <ImageView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingRight="8dp"
        android:paddingTop="8dp"
        android:scaleType="fitXY"
        android:src="@drawable/backicon" />
</LinearLayout>

<ImageView
    android:id="@+id/separator1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/llAppBar"
    android:scaleType="fitXY"
    android:src="@drawable/separatorimg" />

<ListView
    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/separator1"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separatorimg" >
</ListView>

</RelativeLayout>

我也有同样的问题。我是这样解决的:

<ListView
    android:id="@+id/my_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/transparent"
    android:dividerHeight="@dimen/padding_default"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false" >
</ListView>

我使用以下链接的帮助解决了此问题:

我所做的就是添加

  android:footerDividersEnabled="true" 
属性设置为XML格式的ListView。然后,通过编程,我添加了一个空的页脚视图,可以使用

 listView.addFooterView(new View(context), null, true);

我没有尝试标题,但我希望它能以同样的方式工作。

我也遇到了同样的问题,设置填充和背景色解决了这个问题

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_background"
    android:paddingTop="1dp"
    android:paddingBottom="1dp"        
/> 


这里的背景色应该与listview中的分隔器的背景色相同。

谢谢你的回答,不幸的是,这不适合我。首先,视图仅在其下方有另一个元素时显示(不知道为什么),第二,即使在显示时,它始终在屏幕底部可见。我试图达到的效果是,分隔符只出现在列表的末尾,好像它随它一起向上滚动。我尝试了,将代码复制到listview下方的接受答案中(替换listview id)。然后我在屏幕底部得到一条绿线,即使还有很多列表项需要滚动。好的,我的是一些自定义布局,它可以像你想要的那样工作。但我只是复制了你的代码并尝试了,它按照你想要的方式工作。唯一的问题是顶部和底部分隔符不会显示,除非您尝试向下滚动,即使在最后一项之后。您是说我的代码按原样工作(除了仅在尝试滚动过去时显示)还是您的问题的解决方案与我的代码一起工作?因为我甚至看不到绿线,除非我定义了一个高度(例如300dp)而不是包裹内容。我想你添加了线性布局是为了能够使用重量。好主意,但运气不好,列表顶部或底部仍然没有分隔符。此解决方案在滚动时不会消失。对于我来说,分隔符仅在页脚标记为可选择时出现,即
addFooterView
的最后一个参数为
true
。将其设置为
false
不会显示分隔符。
<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_background"
    android:paddingTop="1dp"
    android:paddingBottom="1dp"        
/>