我试图在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>

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


当前回答

为了在listView内部的视图之间提供间距,请在膨胀视图上使用填充。

你可以使用android:paddingBottom="(number)dp" && android:paddingTop="(number)dp"对你的视图或视图,你在你的列表视图内膨胀。

分割线只是一个解决方案,因为有一天,当你想使用分割线的颜色时(现在它是透明的),你会看到分割线被拉伸了。

其他回答

@Asahi几乎一针见血,但我只是想为以后可能通过谷歌在这里浮动的任何人添加一点XML:

<ListView android:id="@+id/MyListView"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:divider="@android:color/transparent"
  android:dividerHeight="10.0sp"/>

由于某些原因,诸如“10”,“10.0”和“10sp”等值都被Android拒绝作为dividerHeight值。它需要一个浮点数和一个单位,比如“10.0sp”。正如@Goofyahead所指出的,你也可以为这个值使用与显示无关的像素(即“10dp”)。

为了在listView内部的视图之间提供间距,请在膨胀视图上使用填充。

你可以使用android:paddingBottom="(number)dp" && android:paddingTop="(number)dp"对你的视图或视图,你在你的列表视图内膨胀。

分割线只是一个解决方案,因为有一天,当你想使用分割线的颜色时(现在它是透明的),你会看到分割线被拉伸了。

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

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

你应该把你的ListView项目(比如your_listview_item)包装在一些其他的布局中,例如LinearLayout,并添加边缘到your_listview_item:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <your_listview_item
        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"
    ...
    ...
    />
</LinearLayout>

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

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

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

这个答案帮助了我。

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