android listview bottom,Top and bottom dividers not showing in Android listview

该问题讨论了在Android ListView中,顶部和底部的分隔线不显示的情况。用户尝试设置`footerDividersEnabled`属性但未生效。其他答案提供了多种解决方案,包括添加空的FooterView,设置透明的分隔符,或者通过修改布局和代码来显示分隔线。问题最终得到了解决,通常是通过添加FooterView并调整相关属性实现的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

The way I understand it, the divider defined for a listview should appear at the top and bottom of the list too, i.e. above the first item and under the last item.

For some reason they don't appear in my listview:

android:id="@+id/mainLayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/background">

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

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

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

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

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

Setting android:footerDividersEnabled to true (which is the answer given in other questions around that matter) doesn't change anything. Also I have no disabled items, unless there is somehow a default footer that would be disabled.

Any ideas?

Edit: It seems the divider at the top and bottom dividers appear for a split second when I scroll past the first/last item, and then disappears. It looks as if interface was going to continue, only later to realize that was the last item and then disable the divider.

回答1:

I solved this problem using help from the following link: Divider disappears from last item in listview if footerview added

What I did was add the

android:footerDividersEnabled="true"

attribute to the ListView in XML. Then programatically, I added an empty footer view that was selectable using

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

I didn't try the header, but I expect it to work the same way.

回答2:

I had the same problem. Here's how I solved it:

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

Then, in the onCreate() method, I have:

View v = new View(this);

v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));

mList.addFooterView(v);

回答3:

I think you have to set footer using a view below the listview. i had the same problem and this is what i could get as a solution:

Show divider after the last item in the list

回答4:

Please find the modified code below :

android:id="@+id/mainLayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/background" >

android:id="@+id/llAppBar"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:orientation="horizontal"

android:weightSum="10" >

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

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

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

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

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

回答5:

I had the same problem, and setting padding and background color solved it.

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"

/>

Here, background color should be same with that of divider with the listview.

来源:https://stackoverflow.com/questions/12139351/top-and-bottom-dividers-not-showing-in-android-listview

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值