我们如何在精确的4行中显示文本,并且只有当文本超出分配的空间(即4行)时才在第4行中引入水平滚动?
我尝试了下面的方法,但是它以单行的方式显示所有内容,并带有水平滚动。如果我注解掉.horizontalScroll(scrollState)
,那么它只显示4行,并且没有滚动。
Text(
text = "In this blog post, " +
"we will explore how to implement a countdown timer" +
" using Jetpack Compose, the modern toolkit for building " +
"native Android UI. We will start with a basic timer and" +
" gradually add more advanced features." +
"we will explore how to implement a countdown timer" +
"using Jetpack Compose, the modern toolkit for building " +
"native Android UI. We will start with a basic timer and" +
"gradually add more advanced features.",
textAlign = TextAlign.Justify,
style = MaterialTheme.typography.bodySmall,
maxLines = 4,
overflow = TextOverflow.Ellipsis,
modifier = Modifier
.horizontalScroll(scrollState)
)
字符串
1条答案
按热度按时间uajslkp61#
要实现只显示4行文本并仅在文本超出分配的空间时才在第4行引入水平滚动的目标,您可以使用
Row
、Column
和Box
的组合沿着使用horizontalScroll
修饰符。下面是一个示例:字符串
Text
的Box
有一个horizontalScroll
修饰符,heightIn
修饰符用于将最大高度限制为计算出的maxHeight
。如果文本的高度超过此限制,将启用水平滚动。否则,文本将显示为4行而不滚动。*