我试图在listView上使用marginBottom来使listView项目之间的空间,但项目仍然附加在一起。

这可能吗?如果有,有具体的方法吗?

我的代码如下

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

<ListView
android:id="@+id/occurences"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

我的自定义列表项:

<com.android.alarm.listItems.AlarmListItem
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"    
>
<CheckedTextView     
    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"

/>

</com.android.alarm.listItems.AlarmListItem>

在这种情况下,我如何使列表项之间的间距?


当前回答

我发现了一个不太好的解决方案,在这种情况下,你正在使用一个HorizontalListView,因为分隔器似乎不与它一起工作,但我认为它将工作为更常见的ListView。

添加:

<View
android:layout_marginBottom="xx dp/sp"/>

在布局的最底部视图中,您在适配器类中膨胀将在项目之间创建间距

其他回答

另外,增加列表项之间间距的另一种方法是向适配器代码中添加一个空视图,方法是为layout_height属性提供所需的间距。例如,为了增加列表项之间的底部间距,将这个虚拟视图(空视图)添加到列表项的末尾。

<View
    android:layout_width="match_parent"
    android:layout_height="15dp"/>

这将在列表视图项之间提供15dp的底部间距。你可以直接添加这个,如果父布局是线性布局和方向是垂直的,或采取适当的步骤为其他布局。希望这对你有所帮助:-)

很多解决方案都是有效的。然而,如果你想要的是能够设置项目之间的空白,我想出的最简单的方法是包装你的项目-在你的情况下CheckedTextView -在一个线性布局,并把你的空白格式为项目,而不是根布局。一定要给这个包装布局一个id,并在适配器中创建CheckedTextView。

就是这样。实际上,您是在ListView的项级别上实例化页边距。因为ListView不知道任何项的布局——只有你的适配器知道。这基本上扩大了之前被忽略的项目布局部分。

OP的现有代码(列表项已经有填充)最简单的解决方案是添加以下代码:

listView.setDivider(new ColorDrawable(Color.TRANSPARENT));  //hide the divider
listView.setClipToPadding(false);   // list items won't clip, so padding stays

这个答案帮助了我。

注意:在旧的平台上,你可能会遇到列表项回收太快的问题,就像这里问的那样。

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

divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >

    <stroke
        android:width="1px"
        android:color="@color/nice_blue" />

</shape>

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

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

</ListView>

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

我使用这个页面作为参考:http://developer.android.com/guide/topics/resources/drawable-resource.html#stroke-element

对于我的申请,我已经这样做了

 <ListView
    android:id="@+id/staff_jobassigned_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:dividerHeight="10dp">

</ListView>

只需要将分隔符设置为null,并为我提供高度。

例子:

android:divider="@null"

or

android:divider="@android:color/transparent"

这就是结果