众所周知,Jetpack Compose中的AnnotatedString已经提供了一些关于Android's SpannedString的API。
但是我没有找到任何方法/解决方案来在文本中内联ImageSpan(除非使用AndroidView)。
众所周知,Jetpack Compose中的AnnotatedString已经提供了一些关于Android's SpannedString的API。
但是我没有找到任何方法/解决方案来在文本中内联ImageSpan(除非使用AndroidView)。
使用AnnotatedString
和Text
Composable的inlineContent
参数可以将图像放置在文本中。
buildAnnotatedString { ... }
中,我们需要使用appendInlineContent(id = ...)
为我们的内联内容定义一些idText
Composable中,在inlineContent
参数中提供一个映射,将此id与InlineTextContent()
对象匹配。基本上,您可以放置任何内容,只要您能够在Placeholder
中预先定义其大小。
val annotatedString = buildAnnotatedString {
append("This is text ")
appendInlineContent(id = "imageId")
append(" with a call icon")
}
val inlineContentMap = mapOf(
"imageId" to InlineTextContent(
Placeholder(20.sp, 20.sp, PlaceholderVerticalAlign.TextCenter)
) {
Image(
imageVector = Icons.Default.Call,
modifier = Modifier.fillMaxSize(),
contentDescription = ""
)
}
)
Text(annotatedString, inlineContent = inlineContentMap)
inlineContentMap
放入一个remember
中。 - mr.iceteastringResource(resId)
添加消息时,remember
不允许使用while
。 - Rahul Rastogi