var showMore by remember { mutableStateOf(false) }
val text =
"Space Exploration Technologies Corp. (doing business as SpaceX) is an American aerospace manufacturer, space transportation services and communications corporation headquartered in Hawthorne, California. SpaceX was founded in 2002 by Elon Musk with the goal of reducing space transportation costs to enable the colonization of Mars. SpaceX manufactures the Falcon 9 and Falcon Heavy launch vehicles, several rocket engines, Cargo Dragon, crew spacecraft and Starlink communications satellites."
Column(modifier = Modifier.padding(20.dp)) {
Column(modifier = Modifier
.animateContentSize(animationSpec = tween(100))
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null
) { showMore = !showMore }) {
if (showMore) {
Text(text = text)
} else {
Text(text = text, maxLines = 3, overflow = TextOverflow.Ellipsis)
}
}
}
7条答案
按热度按时间x8diyxa71#
要解决这个问题,需要使用
onTextLayout
来获得TextLayoutResult
:它包含关于绘制文本的状态的所有信息。使它适用于多行是一项棘手的任务。要做到这一点,你需要计算省略号文本和“...查看更多”文本的大小,然后,当你有两个值时,你需要计算多少文本需要删除,以便“...查看更多”完全适合行尾:
ncgqoxb02#
我的简单实现,希望对大家有用:
a7qyws3x3#
用法
xkrw2x1b4#
一个简单的实现:
6tr1vspr5#
我发现那些贴出来的解决方案有点矫枉过正。下面是一个简单的解决方案:
jw5wzhpr6#
我想要一个更灵活的
第一节第一节第一节第一节第一次
yyyllmsg7#
我有一个here的实现,就像其他人说的,我们应该使用onTextLayout来获取必要的度量,比如文本宽度等。在我的示例中,我尝试通过记住必要的值来最小化重新组合
重新组合和跳过计数:https://user-images.githubusercontent.com/23420470/210508144-3f8de75a-bc5f-4410-b0dc-ba5c4fb399a7.mov