我有一个在适配器上使用的布局行,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title_related"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini Judul Related" />
<TextView
android:id="@+id/time_related"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:text="Jumat, 21 Desember 2018 04:45"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="@+id/image_related"
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_weight="2"
android:paddingEnd="10dp"
android:src="@drawable/picture"
tools:ignore="RtlSymmetry" />
</LinearLayout>
我的问题是,足够大的布局之间始终存在间隙。即使我已经删除了所有填充,比如左右填充。有什么我没看到的吗?
此外,显示看起来像这样:
您可以复制此布局代码 fragment 并将其粘贴到您的测试布局以查看问题。
最佳答案
问题出在您的 ImageView
上。您已设置 android:layout_height="200dp"
,这将强制 View 的高度至少为 200dp
,而不管图像的实际大小。由于您的父布局是 android:layout_height="wrap_content"
,您将获得上述行为。
解决这个问题的一种方法是在 ImageView
中设置 android:layout_height="wrap_content"
。这种方法的缺点是,您无法控制 ImageView 的高度。如果实际图像高度类似于 300dp
,那么 View 将会很大。
另一种方法是降低 ImageView
的高度。仅当您想要对所有图像强制执行特定高度时才执行此操作。
关于android - 如何消除我的适配器行布局上的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54084396/