配置文本布局

本页面介绍了如何使用 maxLinesoverflow 等参数配置文本布局。

限制可见行数

如需限制 Text 可组合项中的可见行数,请设置 maxLines 参数

@Composable
fun LongText() {
    Text("hello ".repeat(50), maxLines = 2)
}

A long text passage truncated after two lines

指示文本溢出

当限制长文本时,您可能需要指示 TextOverflow,仅当显示的文本被截断时才会显示该文本。为此,请设置 textOverflow 参数

@Composable
fun OverflowedText() {
    Text("Hello Compose ".repeat(50), maxLines = 2, overflow = TextOverflow.Ellipsis)
}

A long passage of text truncated after three lines, with an ellipsis at the end