我试图在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:id="@+id/custom_list"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
         android:divider="#00ffffff"
         android:dividerHeight="20dp"/>

其他回答

这将帮助您添加分隔高度。

 getListView().setDividerHeight(10)

如果你想添加一个自定义视图,你可以在listView项目布局本身中添加一个小视图。

也许你可以尝试在最外层的布局中添加android:layout_marginTop = "15dp"和android:layout_marginBottom = "15dp"

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

添加:

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

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

我意识到答案已经被选择了,但我只是想分享当我遇到这个问题时,最终对我有用的方法。

我有一个listView,其中listView中的每个条目都由其自己的布局定义,类似于Sammy在他的问题中发布的内容。我尝试了建议的改变分隔线高度的方法,但最终看起来并不太漂亮,即使使用了一个隐形的分隔线。经过一些实验后,我简单地添加了一个android:paddingBottom="5dip"到XML文件中定义单个listView条目的最后一个TextView布局元素。

这最终给了我什么,我试图通过使用android实现:layout_marginBottom。我发现这个解决方案产生了一个更美观的结果,而不是试图增加分隔高度。

你应该把你的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项的右侧和左侧添加空间。