首页 文章

listView Items Android之间的间距

提问于
浏览
350

我尝试在listView上使用marginBottom来在listView Item之间创建空格,但仍然将这些项连接在一起 .

它甚至可能吗?如果是,是否有特定的方法来做到这一点?

我的代码如下

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

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

17 回答

  • 11

    你只需要使列表分隔符的背景透明,并根据你所需的差距制作高度 .

    <ListView 
             android:id="@+id/custom_list"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:divider="#00ffffff"
             android:dividerHeight="20dp"/>
    
  • 3

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

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

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

  • 789

    很多这些解决方案都有效 . 但是,如果您只想设置项目之间的边距,我提出的最简单的方法是将您的项目(在您的情况下为CheckedTextView)包装在LinearLayout中,并将该项目的边距格式设置为,不是根布局 . 请务必将此包装布局设为id,并将其与适配器中的CheckedTextView一起创建 .

    而已 . 实际上,您要在ListView的项目级别实例化边距 . 因为ListView不知道任何项目布局 - 只有你的适配器 . 这基本上会膨胀之前被忽略的项目布局部分 .

  • 41

    虽然Nik Reiman的解决方案可以解决,但我发现它并不是我想做的最佳解决方案 . 使用分隔符设置边距的问题是分隔符将不再可见,因此您无法使用它来显示项目之间的清晰边界 . 此外,它不会为每个项目添加更多“可点击区域”,因此如果您想让您的项目可点击并且您的项目很薄,那么任何人都很难点击一个项目,因为分隔符添加的高度不是项目的一部分 .

    幸运的是,我找到了一个更好的解决方案,允许您同时显示分隔线,并允许您使用非边距而不是填充来调整每个项目的高度 . 这是一个例子:

    列表显示

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

    如果你想显示一个带边距的分隔线并且没有拉伸它 - 使用InsetDrawable(大小必须是格式,关于哪个@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"/>
    

    @绘制/ 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>
    
  • 8

    您可以使用:

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

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

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

    只是 set the divider to null 并为分隔线提供了高度 .

    Example :

    android:divider="@null"
    

    or

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

    这是 result

    enter image description here

  • 1

    为了在listView中的视图之间给出间距,请在膨胀视图上使用填充 .

    您可以在视图中使用 android:paddingBottom="(number)dp" && android:paddingTop="(number)dp" ,或者在列表视图中查看您的文件 .

    分隔符解决方案只是一个修复,因为有一天,当你想要使用分频器颜色(现在它是透明的)时,你会看到分隔线被拉伸 .

  • 2

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

    只需添加:

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

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

  • 16

    也许 ListViewdividerdividerHeight属性可以解决您的问题 .

  • 60

    我意识到已经选择了一个答案,但我只想分享在我遇到这个问题时最终为我工作的内容 .

    我有一个listView,其中listView中的每个条目都由它自己的布局定义,类似于Sammy在他的问题中发布的内容 . 我尝试了改变分隔符高度的建议方法,但即使使用不可见的分隔符,也不会看起来太漂亮了 . 经过一些实验,我只是将 android:paddingBottom="5dip" 添加到XML文件中的最后一个TextView布局元素,该元素定义了各个listView条目 .

    这最终通过使用 android:layout_marginBottom 给了我我想要实现的目标 . 我发现这种解决方案比试图增加分隔高度产生更美观的结果 .

  • 2

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

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

    This SO回答帮助了我 .

    Note: 您可能会面临旧版平台上过早回收列表项的错误,如here所述 .

  • 4

    而不是给予保证金,你应该给填充:

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

    要么

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingTop="2dp"
        android:divider="@android:color/green"
        android:dividerHeight="4dp"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingBottom="2dp"
        android:paddingStart="0dp"
        android:paddingEnd="0dp" >
    
    </ListView>
    
  • 1
    <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>
    

    并将 paddingToppaddingBottomdividerHeight 设置为相同的值以获得相等列表顶部和底部的所有元素和空间之间的间距 .

    我将 clipToPadding 设置为 false ,以便在此填充区域中绘制视图 .

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

  • 5

    您应该将 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 项目的右侧和左侧添加空间 .

  • 2

    @Asahi几乎触手可及,但我只想为以后通过google浮动的人添加一些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"/>
    

    出于某种原因,Android拒绝 dividerHeight 值等"10","10.0"和"10sp"等值 . 它需要浮点数和单位,例如"10.0sp" . 正如@Goofyahead所说,您还可以使用与显示无关的像素作为此值(即"10dp") .

  • 3

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

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

相关问题