如何使LinearLayout中的TextView默认填充满空间且文本过长时可完整显示?
解决方法
直接修改TextView的属性即可实现需求:
- 把
android:layout_width设为match_parent,让背景水平填满外层LinearLayout的空间 - 添加
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
相关产品推荐
相关产品推荐

