AndroidTextView widget是用来在你的应用程序中显示文本的。
当你有一个长于一行的文本时,那么TextView 会自动将文本放在多行中。
例如,假设你有一个LinearLayout ,其中有一个长的TextView 文本,如下所示。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin placerat mi eget magna volutpat convallis. Vivamus justo leo, accumsan ac vulputate vitae, gravida quis mauris." />
</LinearLayout>
那么Android会自动拉伸TextView ,以显示文本,如下图所示:

Android TextView拉伸以显示所有文本
你也可以通过在你的文本中添加\n 语法来手动添加断裂空间,如下:

带有断裂空间的Android TextView
当你把layout_width 和layout_height 设置为wrap_content 或match_parent ,那么TextView 小组件将使用所有可用的空间来显示你指定的文本作为其内容。
但如果你为小组件的layout_width 和layout_height 属性设置了一个特定的值,则是例外。TextView 将不会拉伸以包括所有的文本。
例如,设置宽度和高度如下所示:
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="..." />
将产生以下输出:
具有固定宽度和高度的Android TextView
](android-textview-fixed-width-height.png)
固定的宽度和高度会使TextView ,将文本截短,在固定的区域内显示一部分文本。
控制TextView的多行行为
有三个安卓属性可以用来帮助你设置TextView 的行为,以便对一个伸展多行的文本:
maxLinesminLines- 和
ellipsize属性。
maxLines 属性用于设置一个TextView widget可以拥有的最大行数。
当你把maxLines 属性设置为2 :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="..." />
那么TextView 将在第二行被填满时截断文本,如下图所示:

带有maxLines属性的Android TextView
你可以通过设置maxLines 的值为1 来防止TextView 创建多行。
当你使用maxLines 属性时,你的TextView 的全文将被截断,留下一个缺口,可能会引起混乱。
ellipsize 属性可以用来在你的文本末尾添加三个尾部的点(...)。这将是一个指示器,让人们知道该文本并不完整。
如下图所示,将ellipsize 属性设置为end 值:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="..." />
你会看到下面的输出:
带有ellipsize属性的Android TextView
最后,你还可以使用minLines 属性来设置一个TextView 必须有的最小行数。
让我们看一个关于minLines 属性的例子。
假设你有三个TextView widgets在一个LinearLayout ,定义如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:background="#D2F8DF"
android:text="Text one \nText two"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="2"
android:background="#C5EAFA"
android:text="Text one \nText two"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="1"
android:background="#FDEAFD"
android:text="Text one \nText two"/>
</LinearLayout>
上述布局将产生以下输出:

带有minLines属性的Android TextView
正如你所看到的,minLines 将导致TextView 至少与该属性的值一样高。
当你的文本使用较少的面积来显示时,TextView 会留下一个空隙,如顶部的TextView 小组件所示。
中间的TextView 内容正好是两行,所以整个TextView 空间被占用。
底部的TextView 显示,当你的文本比minLines 属性的行数多时,那么该小组件将伸展以容纳你的文本。
现在你已经学会了如何使用TextView Android小组件来显示多行的文本。
你还学会了如何使用maxLines,ellipsize, 和minLines 属性来控制TextView 小部件的行为。干得好!👍