如何在Android ListView中添加项目之间的间距?

12
我想在listview中的每个项目之间添加padding,但我想保留默认的分隔符,因为我认为这样更美观。任何更宽的东西看起来都很丑陋。
我目前有的是:
<com.example.practice.MyListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:layout_below="@id/name" />
现在,我已经尝试使用透明的分隔符,它成功地获得了我想要的间距,但是我看不到那条小线了。如果我不使用透明的分隔符,那么我就会有一条又大又丑的粗线。我希望保留默认显示的线,并只在每个列表项的顶部添加一些间距。

3
请在您的列表视图项的XML中进行填充。 - Hoan Nguyen
你的listview item有xml吗? - Hoan Nguyen
我们不是在谈论你上面的ListView。我们正在谈论列表中行的布局。如果您使用android.layout.simple....,那么请编写自己的布局并在那里添加填充。 - Hoan Nguyen
只需创建一个带有一个文本视图的LinearLayout即可。这就是simple_list_item_1的作用。 - Hoan Nguyen
对于列表视图,它的工作是:android:dividerHeight="10dp"。 - Md Imran Choudhury
显示剩余4条评论
7个回答

31

那么你想要实现的并不像那么简单。

步骤一:将分隔线设为透明,并稍微增加高度:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/transparent"
    android:dividerHeight="8dp"/>

第二步:为了实现“小横线”效果,您可以将自定义可绘制项作为列表视图项的背景添加,比如列表视图项定义为“list_item.xml”:

Step Two: In order to achieve the 'little line' effect, you can add a custom drawable as the list view item background, say the list view item is defined as 'list_item.xml':

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<-- Add a Custom background to the parent container of the listview items -->
android:background="@drawable/list_item_selector"
android:orientation="vertical" >
<-- Rest of the item layout -->
</LinearLayout>
当然,那个项目可以是你喜欢的任何东西,我的是:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
  <shape 
    android:shape="rectangle">
        <stroke android:width="1dp" android:color="@color/bg_gray" />
        <solid android:color="@android:color/white" />
    </shape>
</item>
<item android:bottom="1dp"> 
    <shape 
        android:shape="rectangle">
        <stroke android:width="1dp" android:color="#FFDDDDDD" />
        <solid android:color="#00000000" />
    </shape>
</item>
</layer-list>

但那样将禁用“Holo Selector”效果,每当您在listview上单击或突出显示项目时,都会在其上绘制Holo Blue颜色,这就是为什么如果您注意到列表项背景,我们没有使用图层列表可绘制,而是使用名为“list_item_selector”的选择器。

以下是该选择器,当未被按下时,它使用图层列表可绘制,而当被按下时,则使用Holo-blue颜色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_pressed="false"
    android:drawable="@drawable/list_item_bg2"        
    />
 <item android:state_pressed="true"
    android:drawable="@color/holo_blue"
    />
</selector>

评论编辑

完全有可能,您可以为列表视图项定义一个固定的高度,但是建议设置一个最小高度,而不是预定义的永远不改变的高度。

假设这是我的列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/grid_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/grid_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:gravity="center_vertical"
    android:layout_toRightOf="@+id/grid_image"
    android:minHeight="48dp"
    android:padding="8dp"
    android:textIsSelectable="false" />

</RelativeLayout>

所需要的仅是:

第一步:在位于values/文件夹中的dimens.xml文件中定义最小高度或最大高度,根据您的喜好选择。为什么?因为高度应该根据布局而定,您可以为每个设备密度定义不同的dimens.xml。

在dimens.xml中,写入:

<resources>

<!-- List Item Max and Min Height -->

    <dimen name="list_item_min_height">48dp</dimen>
    <dimen name="list_item_max_height">96dp</dimen>
    <dimen name="list_item_set_height">64dp</dimen>

</resources>

然后使用任何值作为你的列表项布局父级 LinearLayout 的值。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_item_min_height" >

这就是关于这个主题的全部内容,现在来居中文本,更简单了:

  • 如果您正在使用 TextView 并将其包装到 RelativeLayout 中,请使用:android:layout_centerVertical="true"

  • 如果您正在使用 LinearLayout,请使用:android:layout_gravity="center_vertical"

再强调一下:注意: 只有当您没有将高度设置为 wrap_content 时,这才起作用,否则它无关紧要。

  android:gravity="center_vertical"

希望这能有所帮助。


我喜欢这个想法。每个项目都添加一个默认大小怎么样?并让文本居中显示?这是否可能? - worm
好东西!感谢帮助。 - worm
设置一个9-patch作为分隔符可绘制对象,并使用适当的分隔符高度来实现所需的间距,这样不是更简单、更优雅的解决方案吗?这样你就不必再去处理选择器和项目背景了。 - MH.

1
我不确定我是否准确理解了你的问题。如果你想让分隔符透明,这样在每个ListView之间可以看到背景的一部分,从而在滚动时产生一种3D效果。你可以这样做:在list_item的xml文件中给LinearLayout设置你想要的背景颜色,例如:
<LinearLayout 
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="match_parent"    
android:padding="4dip"
android:orientation="horizontal"
android:background="#FFFFFF"
>

然后像这样为您的ListView设置背景颜色:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentListView"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="8dip"
android:background="#0000FF"
android:cacheColorHint="@android:color/transparent"
android:drawSelectorOnTop="true"
/>

现在您的 ListView 可以在背景上滚动。
希望这是您想要的。

1
另一种增加列表项间距的方法是通过在适配器代码中添加空视图来提供所需的间距,为 layout_height 属性赋值。例如,如果要增加列表项之间的底部间距,请将此虚拟视图(空视图)添加到列表项末尾。
<View
android:layout_width="match_parent"
android:layout_height="15dp"/>

所以这将为列表视图项提供15dp的底部间距。如果父布局是LinearLayout并且方向是垂直的,则可以直接添加此内容,或者针对其他布局采取适当的步骤。希望这能帮到您 :-)

1
你可以简单地使用分隔线。
请参考以下示例。
  <ListView
    android:id="@+id/activity_main_listview_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"
    />

在这里,你可以通过android:divider来设置颜色或使其透明,在dividerHeight中添加间距以分隔项目。

0
在XML文件中的ListView标签内添加一个dividerHeight标签,并给它一个值(您希望在列表项之间设置的间距)。
这将为您提供适当的列表项间距。
代码:
<ListView
    android:id="@+id/listid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:divider="@drawable/divi"
    android:dividerHeight="60dp"></ListView>

现在创建一个可绘制的XML文件(在这种情况下名称为divi)。在其中添加一个stroke标签并给它一个宽度即可。
代码:
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="#000000"
         />
</shape>

0
这是一个解决方案,适用于那些不想看到分隔符但仍想添加更多空间的人。要完全摆脱分隔符,请将其设置为@null,并将dividerHeight设置为0dp。这是我的通用ListView示例。
<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:layout_alignLeft="@+id/txtMake"
    android:layout_below="@+id/txtMake"
    android:fadeScrollbars="false"
    android:scrollbarThumbVertical="@drawable/scroll_bar_color"
    android:scrollbarStyle="outsideInset" >
</ListView>

接下来,进入包含使用适配器填充列表视图的xml文件。进入您的容器(例如RelativeLayout...),然后简单地添加以下内容。
    android:layout_marginTop="15dp"

这将为那些没有使用分隔符的人添加空间。与仅增加框大小的填充不同,这将增加每个项目之间的距离。


0
android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
android:textSize="30dp"
android:layout_height="wrap_content"

完成这里放代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接