android中ListView项目的边距

android中ListView项目的边距,android,android-layout,android-listview,Android,Android Layout,Android Listview,我试图(徒劳地)为我的ListView项目添加边距。我试着在下面的RelativeLayout中添加边距值,但无论我做什么,我似乎都会在每个项目之间得到一条1px的线 我真正想要的是在每个项目上都有圆角,一个1px的黑色边框和3-5px的左边、上面和右边的边距,但现在我只满足于在每个项目周围留一个边距:-) 我如何实现我的目标?只是现在的利润…;-) 以下是我所拥有的: 更新:我已经更新了下面的xml,删除了主布局和片段布局。我还更新了ListView项目布局,使之与我现在拥有的更加接近,但仍然

我试图(徒劳地)为我的ListView项目添加边距。我试着在下面的RelativeLayout中添加边距值,但无论我做什么,我似乎都会在每个项目之间得到一条1px的线

我真正想要的是在每个项目上都有圆角,一个1px的黑色边框和3-5px的左边、上面和右边的边距,但现在我只满足于在每个项目周围留一个边距:-)

我如何实现我的目标?只是现在的利润…;-)

以下是我所拥有的:

更新:我已经更新了下面的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>

这给了我下面的内容,你会注意到每个项目的左右两侧都有一条非常小的线。我也希望摆脱它


在适配器中,在getView()中捕获相对布局,然后给出布局参数

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

我不擅长布局,但我注意到在过去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>

通常,可以删除只有一个子级的布局,但正如您所看到的,这一个子级有一个用途:


最外面的布局是蓝色,文本视图是红色,绿色是额外的布局,允许您添加一些额外的间距。注意填充(左边的绿色)和边距(右边没有绿色)之间的区别。您已明确表示希望使用边距(
android:layout\u margin
),但您的代码明确使用了填充(
android:padding
),因此我将两者都包括在内。

有关原因,请参见此处的答案。简而言之,子级请求父级,列表视图行使用不包含边距的
AbsListView.LayoutParams


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

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

我假设您在某个XML文件中定义了一个
列表视图
,如果是这样,您可以向它添加一些
填充
,以便在屏幕边缘和列表视图之间有一些空间

例如:

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


您想要边距还是填充?创建圆角和边框并不难,这解释了一些细节。我想要边距。我希望列表项分开。您不需要以编程方式设置页边距。通过XML就足够了。您是否尝试在您的项目的相对值中添加边距?(并在
getView()
中删除dat
getlayoutParams
/
setlayoutParams
)是。我也取得了一些成功。我在ListView项目布局中添加了一个额外的RelativeLayout,这样我现在在RelativeLayout中就有了一个RelativeLayout。将更新问题。谢谢。我会试一试的。没有办法通过XML做到这一点?如果是这样的话,为什么不呢?我不太清楚,但我以前用xml尝试过。我也没有为自己工作。所以我解决了这个问题,在适配器中添加了边距。谢谢Sam。我想我已经完成了您描述的或多或少的工作(添加了一个额外的布局)。AFAICS您对右边的线条有同样的问题(也可能是左边)。你知道怎么去掉这些吗?你说的“非常小的线”是什么意思?您可以通过将
android:divider
更改为空或透明图像,或可能设置
android:dividerHeight=“0dp”
来删除这些分隔符。在外部相对性页签上填充的原因等于在内部相对性页签上留有边距。。。不是吗?我将尝试反转它。是的:-)这些是我的意思:-)我忘记提到的,是ListView的属性,而不是行布局。