android设置listview间距,如何在Android中的listView项目之间设置空间

我尝试在listView上使用marginBottom在listView Item之间留出空间,但这些项目仍附加在一起。

可能吗 如果是,是否有特定方法可以做到?

我的代码如下

android:id="@+id/alarm_occurences"

android:layout_width="fill_parent"

android:orientation="vertical"

android:layout_height="fill_parent"

android:background="#EEEEFF"

xmlns:android="http://schemas.android.com/apk/res/android">

android:id="@+id/occurences"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

我的自定义列表项:

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/alarm_item_background"

android:layout_marginBottom="10dp"

>

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center_vertical"

android:checkMark="?android:attr/listChoiceIndicatorMultiple"

android:textSize="20sp"

android:textStyle="bold"

android:typeface="serif"

android:padding="10dp"

/>

在这种情况下,如何在列表项之间留出间距?

#1楼

您应该将ListView项(例如your_listview_item )包装在其他布局中,例如LinearLayout ,并向your_listview_item添加边距:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/list_item"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginTop="5dp"

android:layout_marginBottom="5dp"

android:layout_marginLeft="5dp"

android:layout_marginRight="5dp"

...

...

/>

这样,您还可以根据需要在ListView项的右侧和左侧添加空间。

#2楼

我意识到已经选择了一个答案,但是我只想分享在遇到此问题时最终对我有用的答案。

我有一个listView,其中listView中的每个条目都由其自己的布局定义,类似于Sammy在他的问题中发布的内容。 我尝试了建议的更改分隔线高度的方法,但即使使用了不可见的分隔线,也没有看起来太漂亮。 经过一番试验后,我只是将android:paddingBottom="5dip"到XML文件中定义单个listView条目的最后一个TextView布局元素中。

这最终给了我确切的我想要通过使用android:layout_marginBottom实现的android:layout_marginBottom 。 我发现此解决方案比试图增加分隔线的高度产生了更美观的结果。

#3楼

尽管Nik Reiman的解决方案确实可行,但我发现它并不是我想要做的最佳解决方案。 使用分隔线设置边距存在一个问题,即分隔线将不再可见,因此您无法使用它来显示项目之间的清晰边界。 另外,它不会为每个项目添加更多的“可点击区域”,因此,如果您要使项目可点击并且项目较薄,那么任何人都很难单击项目,因为分隔线所增加的高度不是项目的一部分。

幸运的是,我找到了一个更好的解决方案,它既可以显示分隔线,又可以使用边距而不是填充来调整每个项目的高度。 这是一个例子:

列表显示

android:id="@+id/listView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

项目清单

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="10dp"

android:paddingTop="10dp" >

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:text="Item"

android:textAppearance="?android:attr/textAppearanceSmall" />

#4楼

如果要显示带边距且不拉伸的分隔线,请使用InsetDrawable(大小必须为格式,@ Nik Reiman表示该格式):

列表显示:

android:id="@+id/listView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:cacheColorHint="#00000000"

android:divider="@drawable/separator_line"

android:dividerHeight="10.0px"/>

@ drawable / separator_line:

android:insetLeft="5.0px"

android:insetRight="5.0px"

android:insetTop="8.0px"

android:insetBottom="8.0px">

android:shape="rectangle">

android:startColor="@color/colorStart"

android:centerColor="@color/colorCenter"

android:endColor="@color/colorEnd"

android:type="linear"

android:angle="0">

#5楼

我增加空间但保持水平线的解决方案是在res/drawable文件夹中添加divider.xml在其中定义线形:

Divider.xml

android:shape="line" >

android:width="1px"

android:color="@color/nice_blue" />

然后在我的列表中引用如下的分隔符:

android:id="@+id/listViewScheduledReminders"

android:layout_width="match_parent"

android:layout_height="0dip"

android:layout_marginBottom="@dimen/mediumMargin"

android:layout_weight="1"

android:divider="@drawable/divider"

android:dividerHeight="16.0dp"

android:padding="@dimen/smallMargin" >

通过增加和减小此高度来注意到android:dividerHeight="16.0dp" ,我基本上是在分隔线的顶部和底部添加更多填充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值