我正在尝试(徒劳)为我的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>
这给了我以下内容,您会注意到每个项目的右侧和左侧都有一条很小的线。我也想摆脱它。
答案 0 :(得分:64)
我对布局不太满意,但我在过去注意到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
),因此我将两者都包括在内。
答案 1 :(得分:15)
有点迟到,但只需将以下行添加到ListView xml元素
android:divider="#00000000"
android:dividerHeight="10dp"
答案 2 :(得分:12)
请在此处查看答案。简而言之,子请求父级,列表视图行使用AbsListView.LayoutParams
,其中不包含边距。
Why LinearLayout's margin is being ignored if used as ListView row view
答案 3 :(得分: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);
答案 4 :(得分:1)
我想你在某个地方的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"/>