ListView 中带有分隔符的项之间的距离

3

在Stack Overflow上,我有几个关于这个主题的问题,但所有这些问题都与ListView项之间的距离相关,几乎所有答案都建议使用透明分隔符作为解决方案。

但在我的情况下,我已经在两个列表项之间有分隔符,那么我该如何在两个列表项之间应用一些外边距/填充/距离呢?

任何帮助都将不胜感激。

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listMainPlans"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/list_divide"
        android:dividerHeight="2dp" >
    </ListView>

</LinearLayout>

1
取决于您要实现的布局。 您可以在XML中创建/膨胀自定义列表项,并添加所需的填充/边距。 - DroidBender
你一定是在将一个布局作为子布局进行填充,对吧? - AkashG
两个文本框,以相对布局方式并排放置。 - GAMA
我在谈论这个布局。只需给定静态高度,您就可以得到每行项目之间的距离。 - AkashG
你需要给 Row XML 的父布局添加 padding。就是这样。 - MKJParekh
显示剩余4条评论
2个回答

4
根据这个教程,您可以向listview_item_row.xml中添加padding。使用您的适配器,可以为ListView中的每个元素填充此xml(在教程中的WeatherAdapter.java)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"> // <-----

     <ImageView  />

     <TextView  />

</LinearLayout>

通过将父相对布局的上下内边距设置为适当的值,它可以正常工作。 - GAMA
还有另一种方法:我可以添加一个带有固定高度的虚拟文本视图。 - GAMA
将空的“TextView”添加到布局中不是最好的工作方式 :) - DroidBender
是的,我知道这个虚拟TextView的方法,但我正在寻找实际解决方案。非常感谢! - GAMA
@Martin 在相对布局中,android:layout_alignParentTop="true"和android:layout_alignParentBottom="true"比在线性布局中更有效,请尝试使用相对布局。 - AkashG

1

给那个相对布局设置静态高度,例如:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_innerbg">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/item_background"
        android:minHeight="40dp">

    <TextView 
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:textColor="@color/blue"
        android:layout_marginRight="45dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textStyle="bold"/>



    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_centerInParent="true"
         android:layout_marginRight="10dp"/>

    </RelativeLayout>

</RelativeLayout>

您将获得每行项目之间的距离


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