ConstraintLayout实现RecyclerView单元格时TextView文本截断问题求助
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_heightto0dp(match constraints) - You pinned its top to the top of
cell_thumband bottom to the top ofcell_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:
- Change
cell_summary's height towrap_contentto let it grow with text - Update
cell_datetime's top constraint to attach to the bottom ofcell_summary - Keep
cell_thumbaligned to the total height of the text section by maintaining its bottom constraint tocell_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: Switchedlayout_heighttowrap_contentso it expands vertically to fit all text. Removedellipsize="end"(since you don't want truncation) and the bottom constraint that was limiting its height.cell_datetime: Updatedapp: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: Keptapp: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, replaceandroid:layout_height="88dp"withapp: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

