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

如何使LinearLayout中的TextView默认填充满空间且文本过长时可完整显示?

解决方法

直接修改TextView的属性即可实现需求:

  1. 把android:layout_width设为match_parent,让背景水平填满外层LinearLayout的空间
  2. 添加android:singleLine="false"(默认不设置也支持多行),保证长文本自动换行完整显示

修改后的TextView代码:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/week"
    android:gravity="center"
    android:text="Text"
    android:textColor="@color/white"
    android:singleLine="false" />

外层LinearLayout保持android:layout_width="wrap_content"即可,它会根据TextView的内容自动调整宽度,TextView的match_parent会填满这个宽度,既满足背景铺满的要求,又能完整显示长文本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:15:28