RecyclerView的CardView中两个TextView发生重叠如何解决?
问题根因
你为id为name的TextView设置了android:layout_width="wrap_content",在ConstraintLayout布局规则下,当控件左右两侧都添加了约束、但宽度设为wrap_content时,如果文本内容长度超出左右约束之间的可用宽度,控件会直接突破约束展示完整内容,不会自动适配边界,因此会覆盖右侧固定的status控件。
修复方案
仅需要修改nameTextView的两个核心属性即可解决重叠问题:
- 将
android:layout_width="wrap_content"改为android:layout_width="0dp",在ConstraintLayout中0dp代表匹配约束宽度,会让控件严格限制在左右约束之间的区域内展示 - 可选补充:增加文字溢出处理属性,避免长文本换行打乱卡片统一高度:
android:maxLines="1" android:ellipsize="end"
可选优化项
所有约束尽量使用start/end代替left/right,可以自动适配阿拉伯语等从右到左的语言场景,修改点如下:
- status控件的
app:layout_constraintRight_toRightOf="parent"改为app:layout_constraintEnd_toEndOf="parent",android:layout_marginRight改为android:layout_marginEnd - name控件的
app:layout_constraintRight_toLeftOf="@+id/status"改为app:layout_constraintEnd_toStartOf="@+id/status",android:layout_marginLeft改为android:layout_marginStart
修正后的name控件完整代码示例
<TextView style="@style/headline_6" android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:textColor="@color/col_gray" app:layout_constraintStart_toEndOf="@+id/left_image" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toStartOf="@+id/status" android:layout_marginStart="@dimen/spacer_s" android:layout_marginTop="@dimen/s_base" android:text="Dummy Dummy Dummy Dummy Dummy Dummy" android:gravity="center_vertical" android:maxLines="1" android:ellipsize="end"/>
内容的提问来源于stack exchange,提问作者VVB
相关产品推荐
相关产品推荐

