android中ListView项目的边距 [英] Margin on ListView items in android

查看:22
本文介绍了android中ListView项目的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试(徒劳)为我的 ListView 项目添加边距.我尝试在下面的相对布局中添加边距值,但无论我做什么,我似乎得到的都是每个项目之间的 1px 线.

I'm trying (in vain) to add margins to my ListView items. I have tried adding margin values to my RelativeLayout below but no matter what I do all I seem to get is a 1px line between each item.

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

What I really would like is to have rounded corners on each item, a 1px black border and a 3-5px margin left, top, and right but right now I'll settle for just a margin around each item :-)

我如何实现我的目标?只是现在的余量... ;-)

How do I achieve my goals? Just the margin for now... ;-)

这是我所拥有的:

更新:我已经更新了下面的 xml,删除了主布局和片段布局.我还将 ListView 项目布局更新为我现在拥有的,这更接近我想要的但仍然不完美.截图也添加了

UPDATE: I have updated the xml below removing main layout and fragment layout. I have also updated the ListView item layout to what I have now which is closer to what I want but still not perfect. Screenshot added as well

listview 项目布局 xml

listview item layout 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>

这为我提供了以下内容,您会注意到每个项目的左右两侧都有一条非常小的线.我也想摆脱.

This gives me the following where you'll notice a very small line to the right and left for each item. That I would also like to get rid of.

推荐答案

我不太擅长布局,但我过去注意到 ListView 行经常忽略 LayoutParams.我不知道这种情况发生在哪里,或者是否可以覆盖,我知道您可以通过添加另一个布局轻松解决它:

I'm not great with layouts, but I have noticed in the past that ListView rows often ignore LayoutParams. I have no idea where this happens or if it's possible to override, I do know you can easily work around it by adding another layout:

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

通常可以删除只有一个子项的布局,但正如您所见,此布局是有目的的:

Typically layouts that only have one child can be removed, but as you can see this one serves a purpose:

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

The outer-most layout is blue, the TextView is red, and the green is the extra layout that allows you to add some extra spacing. Notice the difference between padding (the green on the left) and margin (no green on the right). You have clearly stated that you want to use margins (android:layout_margin) but your code clearly uses padding (android:padding) so I included both.

这篇关于android中ListView项目的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆