简体   繁体   English

android中ListView项目的保证金

[英]Margin on ListView items in android

I'm trying (in vain) to add margins to my ListView items. 我正在尝试(徒劳)为我的ListView项添加边距。 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. 我已经尝试在下面的RelativeLayout中添加边距值,但无论我做什么,我似乎得到的是每个项目之间的1px线。

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 :-) 我真正想要的是每个项目都有圆角,1px黑色边框和3-5px左边,上边和右边距但现在我只能在每个项目周围找到一个边距:-)

How do I achieve my goals? 我如何实现目标? Just the margin for now... ;-) 只是现在的余地...... ;-)

Here's what I have: 这就是我所拥有的:

UPDATE: I have updated the xml below removing main layout and fragment layout. 更新:我已经更新了下面的xml删除主布局和片段布局。 I have also updated the ListView item layout to what I have now which is closer to what I want but still not perfect. 我还将ListView项目布局更新为我现在拥有的更接近我想要但仍然不完美的内容。 Screenshot added as well 屏幕截图也添加了

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

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. 那我也想摆脱。

在此输入图像描述

I'm not great with layouts, but I have noticed in the past that ListView rows often ignore LayoutParams. 我对布局不太满意,但我在过去注意到ListView行经常忽略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: 通常只删除一个子项的布局,但正如您所看到的那样,这个布局有用:

屏幕截图

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. 最外面的布局是蓝色,TextView是红色,绿色是额外的布局,允许您添加一些额外的间距。 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:layout_margin ),但是你的代码显然使用了padding( android:padding ),所以我包含了两者。

A little late seeing this, but just add the following lines to your ListView xml element 有点迟到,但只需将以下行添加到ListView xml元素中

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

See the answer here for why. 请在此处查看答案。 In short the child asks the parent, and the list view row uses AbsListView.LayoutParams , which doesn't include margins. 简而言之,子请求父级,列表视图行使用AbsListView.LayoutParams ,它不包含边距。

Why LinearLayout's margin is being ignored if used as ListView row view 如果用作ListView行视图,为什么会忽略LinearLayout的边距

In your adapter, catch your relative layout in getView(), then give a layout params , 在你的适配器中,在getView()中捕获你的相对布局,然后给出一个布局参数,

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

I suppose you have a ListView defined in an XML file somewhere, if so, you could add some padding to it, so that there will be some space between the edge of the screen and the ListView. 我想你在某个地方的XML文件中定义了ListView ,如果是这样,你可以为它添加一些padding ,这样在屏幕边缘和ListView之间会有一些空间。

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM