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

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


当前回答

如果你想显示一个带边距的分隔符,而不拉伸它-使用InsetDrawable(大小必须是一种格式,关于它说@Nik Reiman):

ListView:

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

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="5.0px"
    android:insetRight="5.0px"
    android:insetTop="8.0px"
    android:insetBottom="8.0px">

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="@color/colorStart"
            android:centerColor="@color/colorCenter"
            android:endColor="@color/colorEnd"
            android:type="linear"
            android:angle="0">
        </gradient>
    </shape>
</inset>

其他回答

<ListView
    android:clipToPadding="false"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:dividerHeight="10dp"
    android:divider="@null"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

并设置paddingTop, paddingBottom和dividerHeight为相同的值,以获得所有元素之间的相等间距和列表顶部和底部的空间。

我设置clipToPadding为false,让视图在填充区域中绘制。

我将分隔符设置为@null以删除列表元素之间的行。

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

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

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

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

添加:

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

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

如果你想显示一个带边距的分隔符,而不拉伸它-使用InsetDrawable(大小必须是一种格式,关于它说@Nik Reiman):

ListView:

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

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="5.0px"
    android:insetRight="5.0px"
    android:insetTop="8.0px"
    android:insetBottom="8.0px">

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="@color/colorStart"
            android:centerColor="@color/colorCenter"
            android:endColor="@color/colorEnd"
            android:type="linear"
            android:angle="0">
        </gradient>
    </shape>
</inset>

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