You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ConstraintLayout实现RecyclerView单元格时TextView文本截断问题求助

Fixing Truncated Long Text in ConstraintLayout RecyclerView Cell

Hey there, let's figure out why your cell_summary TextView is cutting off long text even when you don't set a max line limit!

The Root Cause

Looking at your layout, the issue boils down to how you've constrained the cell_summary TextView:

  • You set its layout_height to 0dp (match constraints)
  • You pinned its top to the top of cell_thumb and bottom to the top of cell_datetime

This locks the height of cell_summary to exactly the height of cell_thumb minus the height of cell_datetime—so any text longer than that fixed space gets truncated, no matter if you set maxLines or not.

The Solution

We need to let cell_summary expand freely based on its content, then rearrange the constraints so cell_datetime sits below it instead of clamping it from below. Here's how to adjust your layout:

  1. Change cell_summary's height to wrap_content to let it grow with text
  2. Update cell_datetime's top constraint to attach to the bottom of cell_summary
  3. Keep cell_thumb aligned to the total height of the text section by maintaining its bottom constraint to cell_datetime

Modified Layout Code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/cell_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:maxLines="2"
        android:text="And we find ourselves cruelly cut off from the wireless world"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/cell_thumb"
        android:layout_width="120dp"
        android:layout_height="88dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:background="#cccccc"
        android:scaleType="centerCrop"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cell_title"
        app:layout_constraintBottom_toBottomOf="@id/cell_datetime" />

    <TextView
        android:id="@+id/cell_datetime"
        android:layout_width="0dp"
        android:layout_height="15dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="4dp"
        android:text="2Hours"
        android:textColor="#999999"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@id/cell_thumb"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cell_summary" />

    <TextView
        android:id="@+id/cell_summary"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:text="But then we were not being singled out, the entire country lives in a bubble of unreality, cut off from the outside world and watched by an army of informers."
        app:layout_constraintLeft_toLeftOf="@id/cell_datetime"
        app:layout_constraintRight_toRightOf="@id/cell_datetime"
        app:layout_constraintTop_toTopOf="@id/cell_thumb" />

    <View
        android:layout_width="0dp"
        android:layout_height="0.5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="16dp"
        android:background="#dddddd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cell_thumb" />

</android.support.constraint.ConstraintLayout>

Key Changes Explained

  • cell_summary: Switched layout_height to wrap_content so it expands vertically to fit all text. Removed ellipsize="end" (since you don't want truncation) and the bottom constraint that was limiting its height.
  • cell_datetime: Updated app:layout_constraintTop_toBottomOf="@id/cell_summary" so it sits directly below the summary text instead of restricting it. Added a small top margin for better spacing.
  • cell_thumb: Kept app:layout_constraintBottom_toBottomOf="@id/cell_datetime" to ensure the image stretches to match the total height of the summary + datetime section. If you want to keep the image at a fixed minimum height, replace android:layout_height="88dp" with app:layout_constraintHeight_min="88dp".

This setup will let your summary text display fully without any line limits, while keeping the layout properly aligned in your RecyclerView cell.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:08:57