ListView中项目之间的距离

17
我在布局中有一个ListView。
    <ListView 
        android:id="@+id/list_infoitems"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
每个列表项的布局如下:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/selector_custombutton"
    android:padding="10dp"
    android:layout_gravity="center">
    <TextView
        android:id="@+id/text_history_station_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:textColor="@color/rnv_blue_540c"/>
    <TextView
        android:id="@+id/text_history_line_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:textColor="@color/rnv_blue_540c"/>      
</LinearLayout>

我希望在所有项目之间留出距离。为此,我尝试使用android:margin属性,但没有成功。

有人知道如何更改ListView中项目之间的距离吗?

谢谢,

Mur


每一行之间的距离?还是每一行中出现的每个项目之间的距离? - Aman Alam
4个回答

32
你可以始终设置dividerHeight属性。Android文档
<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="5dp"
/>

如果您不想这样做,在您的列表视图项中,您可以将LinearLayout包装在RelativeLayout中,并向LinearLayout添加边距。

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
    />
</RelativeLayout>

我有和你第二个建议一样的想法,但是你的第一个建议更加有帮助且更好 :) 非常感谢你。 - Tima
3
这对我很有帮助,不过我的列表是黑色的,分隔线是灰色的。一定要将分隔线更改为与您的列表相同的颜色!在我的情况下,使用android:divider="#00000"。 - NPike
1
@NPike,或者使用#00000000将分隔符设置为透明。 - softarn

1

我发现ListView分隔符存在碎片化问题。在2.3设备上,默认实现了一个灰色线条,但在4.x设备上却没有显示出来。这可以通过手动设置android:divider="some_value"来解决。我倾向于在列表项布局中处理所有ListView项布局问题。这是一个小技巧,但可以解决跨版本问题。请注意,在下面添加了一个“隐藏”的视图。这不仅可以解决项目之间的问题,还可以在列表中的最后一个项目后提供相等的填充。

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="5dp" />

    <View
        android:below="@id/list"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:margin_top="5dp" />
</RelativeLayout>

0

使用padding或margin或ListView本身。

<ListView 
        android:id="@+id/list_infoitems"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
/>

0

如果您想增加项目之间的距离,可以使用以下方法:

android:dividerHeight="xxdpi"

<ListView 
    android:id="@+id/list_infoitems"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="xxdpi"/>

问候,Marco


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