You need to enable JavaScript to run this app. Android横向RecyclerView如何让TextView宽度与ImageView保持一致
优惠活动
大模型
产品
解决方案
定价
更多

Android横向RecyclerView如何让TextView宽度与ImageView保持一致

解决方案

核心问题原因

你的布局存在两个核心问题导致效果不符合预期:

  • ImageView的id声明为topimage(小写i),但下方TextView引用时写的是topImage(大写I),id不匹配可能导致布局层级错乱
  • 整个Item的宽度没有和你动态设置的图片宽度绑定,TextView使用wrap_content宽度会随文本长度自动拉伸,撑大整个Item宽度,同时没有换行限制属性

第一步:修改Item XML布局

调整两个TextView的宽度属性、换行属性,修正id引用错误:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    app:cardElevation="0dp">

        <FrameLayout
            android:id="@+id/media_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/topimage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:cropToPadding="true"
                android:paddingStart="8dp"
                android:paddingEnd="8dp"
                android:scaleType="fitXY" />

        <TextView
            android:id="@+id/title"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/topimage"
            android:padding="2dp"
            android:text="@string/placeholder_name"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:layout_marginTop="5dp"
            <!-- 可选配置:最多显示2行,超出末尾加省略号 -->
            android:maxLines="2"
            android:ellipsize="end"
            />

        <TextView
            android:id="@+id/subtitle"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:padding="2dp"
            android:text="@string/placeholder_details"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:layout_marginBottom="10dp"
            <!-- 可选配置:最多显示1行,超出末尾加省略号 -->
            android:maxLines="1"
            android:ellipsize="end"
            />

            </RelativeLayout>
        </FrameLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>

注意:如果不需要限制行数,只需要自动换行,删除maxLines和ellipsize属性即可。

第二步:修改Adapter中的动态布局代码

在原有设置图片宽高的逻辑后,新增代码固定整个Item的宽度和图片宽度一致:

double widthDouble = (Resources.getSystem().getDisplayMetrics().widthPixels / 2) * 0.9;
int width = (int) widthDouble;
int height = (int) (width * 0.7);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
viewHolder.topimage.setLayoutParams(lp);
viewHolder.topimage.setScaleType(ImageView.ScaleType.FIT_CENTER);

// 新增:固定整个Item的宽度和图片宽度一致
LinearLayout.LayoutParams itemParams = (LinearLayout.LayoutParams) viewHolder.itemView.getLayoutParams();
itemParams.width = width;
viewHolder.itemView.setLayoutParams(itemParams);

效果说明

修改后整个Item的宽度会和你计算的图片宽度完全一致,TextView的宽度被限制在Item宽度范围内,文本超出后会自动换行或者按配置截断,不会再撑大Item导致图片间距异常。

内容的提问来源于stack exchange,提问作者cesarcarlos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 00:36:08