【发布时间】:2013-02-26 20:35:59
【问题描述】:
我正在尝试(徒劳地)为我的 ListView 项目添加边距。我尝试在下面的 RelativeLayout 中添加边距值,但无论我做什么,我似乎得到的只是每个项目之间的 1px 线。
我真正想要的是在每个项目上都有圆角,一个 1 像素的黑色边框和一个 3-5 像素的左侧、顶部和右侧边距,但现在我会满足于每个项目周围的边距 :-)
如何实现我的目标?只是现在的边距...... ;-)
这是我所拥有的:
更新:我已经更新了下面的 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>
这给了我以下信息,您会注意到每个项目的左右两侧都有一条非常小的线。我也想摆脱它。
【问题讨论】:
-
你想要边距还是内边距?
-
创建圆角和边框并不难,这个Developer's Guide 解释了一些细节。
-
我想要保证金。我希望我的列表项分开。
-
您不需要以编程方式设置边距。通过 XML 应该就足够了。您是否尝试在项目的 relativeLayout 中添加边距? (并在您的
getView()中删除 datgetlayoutParams/setlayoutParams) -
是的。我已经取得了一些成功。我在我的 ListView 项目布局中添加了一个额外的 RelativeLayout,因此我现在在 RelativeLayout 中有一个 RelativeLayout。将更新问题。
标签: android android-layout android-listview