gpt4 book ai didi

android - android中ListView项目的边距

转载 作者:IT老高 更新时间:2023-10-28 21:53:31 24 4
gpt4 key购买 nike

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

我真正想要的是在每个项目上都有圆角,一个 1 像素的黑色边框和一个 3-5 像素的左侧、顶部和右侧边距,但现在我会满足于每个项目周围的边距 :-)

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

这是我所拥有的:

更新:我已经更新了下面的 xml,删除了主布局和 fragment 布局。我还将 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>

这给了我以下信息,您会注意到每个项目的左右两侧都有一条非常小的线。我也想摆脱它。

enter image description here

最佳答案

我不擅长布局,但我过去注意到 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>

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

Screen Shot

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

关于android - android中ListView项目的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15342461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com