如何在Android中设置listView项目之间的空间

如何在Android中设置listView项目之间的空间,android,android-layout,android-listview,Android,Android Layout,Android Listview,我试图在listView上使用marginBottom在listView项之间留出空间,但这些项仍然连接在一起 有可能吗?如果是,有没有具体的方法 我的代码在下面 <LinearLayout android:id="@+id/alarm_occurences" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent" android:backg

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


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

或者
列表视图的属性可以解决您的问题。

@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”等值都会因
dividerHeight
值而被Android拒绝。它需要一个浮点数和一个单位,如“10.0sp”。正如@Goofyahead所指出的,您也可以使用显示独立像素来显示此值(即,“10dp”)。

您应该将
列表视图
项(比如
您的列表视图
)包装在其他一些布局中,例如
线性布局
,并为
您的列表视图
项添加边距:

<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,其中listView中的每个条目都由自己的布局定义,类似于Sammy在他的问题中发布的内容。我尝试了改变分隔器高度的建议方法,但最终看起来并不太漂亮,即使有一个看不见的分隔器。经过一些实验后,我只是在XML文件中定义单个listView条目的最后一个TextView布局元素中添加了一个
android:paddingBottom=“5dip”


这让我完全明白了我试图通过使用android:layout_marginBottom实现的目标。我发现这个解决方案比试图增加分隔高度更美观。

尽管Nik Reiman的解决方案确实有效,但我发现它不是我想要做的事情的最佳解决方案。使用分隔符设置边距时出现了一个问题,即分隔符将不再可见,因此您无法使用它显示项目之间的清晰边界。此外,它不会为每个项目添加更多的“可点击区域”,因此,如果您想使项目可点击且项目较薄,则任何人都很难点击项目,因为分隔器添加的高度不是项目的一部分

 getListView().setDividerHeight(10)
幸运的是,我找到了一个更好的解决方案,它允许您同时显示分隔符,并允许您不使用边距而是使用填充来调整每个项目的高度。以下是一个例子:

列表视图

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

列表项

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <TextView
        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" />

</RelativeLayout>

如果您想显示带边距且不拉伸的分隔符,请使用InsertDrawable(大小必须采用某种格式,上面写着@Nik Reiman):

列表视图:

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

@可抽出/分隔线:

<?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>

我增加更多空间但保持水平线的解决方案是在
res/drawable
文件夹中添加
divider.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”
通过增加和减少这个高度,我基本上在分隔线的顶部和底部添加了更多的填充

我使用此页面作为参考:

您可以使用:

android:divider="@null"
android:dividerHeight="3dp"
例如:

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" android:layout_gravity="center_horizontal"
        android:dividerHeight="3dp"
        android:divider="@null" android:clickable="false"/>

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

listView.setDivider(new ColorDrawable(Color.TRANSPARENT));  //hide the divider
listView.setClipToPadding(false);   // list items won't clip, so padding stays
所以这个答案帮助了我


注意:您可能会遇到一个问题,即旧平台上的列表项回收过快。

您应该提供填充,而不是提供边距:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:divider="@android:color/green"
    android:dividerHeight="4dp"
    android:layout_alignParentTop="true"
    android:padding="5dp" >

</ListView>


为了给列表视图中的视图留出间距,请在充气视图上使用填充

您可以在您的视图上使用
android:paddingBottom=“(number)dp”
和&
android:paddingTop=“(number)dp”
,或在列表视图中充气的视图

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


<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
,以便在该填充区域中绘制视图


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

增加列表项之间间距的另一种方法是,通过提供具有所需间距的布局高度属性,将空视图添加到适配器代码中。例如,为了增加列表项之间的底部间距,将此虚拟视图(空视图)添加到列表项的末尾

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


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

对于我的应用程序,我已经这样做了

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

</ListView>

android:divider="@android:color/transparent"
这就是结果


如果您使用的是HorizontalListView,我发现了一个不太好的解决方案,因为除法器似乎不适用于它,但我认为它对更常见的ListView都适用

只是补充:

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


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

您只需要创建背景
<View
android:layout_marginBottom="xx dp/sp"/>
<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)