android - 如何消除我的适配器行布局上的间隙

标签 android android-layout android-recyclerview

我有一个在适配器上使用的布局行,如下所示:

<?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>

我的问题是,足够大的布局之间始终存在间隙。即使我已经删除了所有填充,比如左右填充。有什么我没看到的吗? 此外,显示看起来像这样: row layout with big gap
您可以复制此布局代码 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/

相关文章:

google-maps - 在非 Activity 类中使用 TextView 资源。 MainActivity 停止并显示 "Exception despatching input event."

android - 想要创建一个在特定区域可点击的半 donut 形状按钮

java - 是否有任何类可以处理 RecyclerView ViewHolders 的数据绑定(bind)?

android - 在 Android 中以编程方式在相对布局中添加多个相对布局

java - Android视频(VideoView)播放时没有音频

java - 单击 ViewPager 中的特定页面后如何跳转到另一个 Activity ?

android - 单击 RecylcerView 中的项目时,ActionMode 出现在当前 ActionBar 的顶部

Android SDK 23 缺少 RecyclerView 和 CardView

java - 排序 Android ListView

android - 拉动以刷新android中的水平 ListView