android中ListView项目的保证金

CJe*_*CJe 30 android android-layout android-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>
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

Sam*_*Sam 66

我对布局不太满意,但我在过去注意到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>
Run Code Online (Sandbox Code Playgroud)

通常只删除一个子项的布局,但正如您所看到的那样,这个布局有用:

屏幕截图

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

  • "非常小的一行"你的意思是[这些](http://i.stack.imgur.com/KnXaV.png)?这些是可以通过将`android:divider`更改为空或透明图像,或者可能设置`android:dividerHeight ="0dp"`来删除它们的分隔符. (2认同)

Zac*_*ers 17

有点迟到,但只需将以下行添加到ListView xml元素中

android:divider="#00000000"
android:dividerHeight="10dp"
Run Code Online (Sandbox Code Playgroud)


use*_*019 12

请在此处查看答案.简而言之,子请求父级,列表视图行使用AbsListView.LayoutParams,不包括边距.

如果用作ListView行视图,为什么会忽略LinearLayout的边距