如何在Android Jetpack Compose中添加删除线?

18

我想在Android Jetpack Compose中给一段文本添加删除线。我查阅了一些文档,但仍然没有找到符合我需求的内容。

https://developer.android.com/jetpack/compose/text

这是我的 Text 组件:

Text("$863",fontSize = 24.sp, modifier = Modifier.width(IntrinsicSize.Min), maxLines = 1)

这是我想要实现的:

在此输入图片描述

请问有人能帮忙或给我任何建议吗?

3个回答

48

为文本添加样式

@Composable
fun TextWithLineThroughExample(){
    Text(
        text = "Text with LineThrough",
        style = TextStyle(textDecoration = TextDecoration.LineThrough)
    )
}

这是一张图片描述


1
谢谢@Tippu,这对我有用。 - Luis Aguilar
1
Text composable 现在接受一个textDecoration参数,因此我们不再需要覆盖样式了。 - undefined

7

我建议复制当前样式并调整属性:

@Composable
fun textWithLineThroughExample(){
    Text(
        text = "Text with LineThrough",
        style = LocalTextStyle.current.copy(textDecoration = TextDecoration.LineThrough)
    )
}

这种方法会保留所有其他现有的样式属性(例如在ProvideTextStyle块内)。

0
现在,Compose直接支持在@Composable Text上进行TextDecoration,这样您就不必复制/覆盖样式了。
@Composable
fun TextWithLineThroughExample(){
    Text(
        text = "Text with LineThrough",
        textDecoration = TextDecoration.LineThrough,
        style = MaterialTheme.typography.bodyLarge, // You can set this separate now
    )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接