Android中ListView项目的间距

30

我试图(徒劳地)为我的ListView项添加边距。我已经尝试向下面的RelativeLayout添加边距值,但无论我做什么,似乎都只会得到每个项之间的一个1像素线条。

我真正想要的是每个项都有圆角、1像素黑色边框和左、上、右各3-5像素的边距,但现在我就满足于每个项周围有一定的边距了 :-)

我该如何实现我的目标?现在只需要边距... ;-)

以下是我的代码:

更新: 我已经更新了以下的xml,删除了主布局和片段布局。我还将ListView项布局更新到我现在的状态,这更接近我想要的,但仍然不完美。屏幕截图也已添加。

listview项布局xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/matchMargin"
android:paddingRight="@dimen/matchMargin"
android:paddingTop="@dimen/matchMargin" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#cfcfcfcf" >

    <include
        android:id="@+id/matchKampstart"
        layout="@layout/kampstart_layout" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/matchKampstart"
        android:layout_marginTop="@dimen/belowKampstartMargin" >

        <ImageView
            android:id="@+id/tournamentImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/tournamentImageViewContentDescription"
            android:gravity="left"
            android:src="@drawable/sofabold_launcher" />

        <ImageView
            android:id="@+id/homeTeamImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/homeTeamImageViewContentDescription"
            android:src="@drawable/sofabold_launcher" />

        <TextView
            android:id="@+id/homeTeam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:text="@string/home"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:gravity="center"
            android:text="@string/dash"
            android:textSize="12sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/awayTeamImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/awayTeamImageViewContentDescription"
            android:src="@drawable/sofabold_launcher" />

        <TextView
            android:id="@+id/awayTeam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="@string/away"
            android:textSize="14sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/matchKampstart"
        android:layout_marginTop="@dimen/belowKampstartMargin" >

        <ImageView
            android:id="@+id/tvChannelImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="false"
            android:contentDescription="@string/tvChannelImageViewContentDescription"
            android:gravity="right"
            android:src="@drawable/sofabold_launcher" />
    </LinearLayout>

</RelativeLayout>

</RelativeLayout>

这给出了以下结果,你会注意到每个项目的右侧和左侧都有一条非常细小的线。我也想摆脱它们。

输入图像描述


1
创建圆角和边框并不太难,这个开发者指南解释了一些细节。 - Sam
我想要边距。我希望我的列表项分开。 - CJe
你不应该需要通过编程来设置边距,通过XML应该就足够了。你尝试在你的项目的RelativeLayout中添加边距了吗?(并且在你的getView()中删除getlayoutParams / setlayoutParams - AMerle
1
是的。我有些成功了。我已经在我的ListView项布局中添加了一个额外的RelativeLayout,所以现在我有一个RelativeLayout内部的RelativeLayout。将更新问题。 - CJe
小线不是你的分隔符吗?如果是的话,只需删除分隔符或将其设置为与您的背景相同的颜色。 - AMerle
显示剩余2条评论
5个回答

70

我不太擅长布局,但我曾经注意到,在过去的一些情况下,ListView行通常会忽略LayoutParams。我不知道这是在哪里发生的,也不知道是否可以覆盖它,但我知道你可以通过添加另一个布局来轻松解决它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="#990000ff" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="#9900ff00"
        android:paddingLeft="10dp" >

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#99ff0000" />
    </LinearLayout>

</LinearLayout>

通常只有一个子元素的布局可以被删除,但是您可以看到这个布局有一定的用途:

屏幕截图

最外层的布局是蓝色的,TextView 是红色的,绿色的是额外的布局,允许您添加一些额外的间距。请注意填充(左侧的绿色)和边距(右侧没有绿色)之间的区别。您已经明确表示要使用边距 (android:layout_margin),但您的代码明显使用了填充 (android:padding),因此我包含了两者。


谢谢Sam。我认为我已经按照你的描述做了(添加了额外的布局)。据我所知,您在右侧(可能也在左侧)的线条上有相同的问题。有什么办法可以摆脱这些? - CJe
2
“非常小的线”是指这些吗?您可以通过更改android:divider为一个空或透明图像,或者可能设置android:dividerHeight ="0dp"来移除它们,这些是分隔符。 - Sam
1
我忘了提到dividerdividerHeight是用于ListView的属性,而不是行布局。 - Sam
我明白了。将dividerHeight设置为零没有任何效果 :-( - CJe
一如既往,每个部分的最后一项都存在问题,这让我不得不通过编程来处理边距 :-/ - CJe
显示剩余5条评论

19

有点晚看到这个消息,但只需将以下行添加到ListView xml元素中即可

android:divider="#00000000"
android:dividerHeight="10dp"

12

4

在你的适配器中,在getView()方法中获取你的相对布局,然后提供一个布局参数。

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom
YourRelativeLayoutInAdapter.setLayoutParams(params);

谢谢。我会尝试一下。没有通过XML实现这个的方法吗?如果有,为什么不用呢? - CJe
1
我不确定具体情况,但之前我尝试使用XML时也没有成功。所以我通过在适配器中添加边距来解决了这个问题。 - Talha

3

假设您在某个XML文件中定义了一个ListView,如果是这样,您可以为其添加一些padding,这样屏幕边缘和ListView之间就会有一些空间。

示例:

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"/>

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