更改撰写中 SelectionContainer 中选择文本的颜色

Bar*_*fet 1 android android-jetpack-compose

我想更改SelectionContainer的颜色,但在参数部分中没有看到任何可以在此处进行更改的选项。我还查看了内部代码,但看不到任何与颜色相关的内容。文档没有提到这一点。

问题:有没有办法改变选择颜色,或者这是不可能的?

clm*_*mno 6

您可以使用TextSelectionColors设置所选内容的颜色。

为此,您需要将您的内容包装TextCompositionLocalProvider. 文本和文本字段组件用于文本选择的颜色由LocalTextSelectionColors.current提供。

您可以TextSelectionColors使用以下方式提供自定义:

val customTextSelectionColors = TextSelectionColors(
    handleColor = Red,
    backgroundColor = Red.copy(alpha = 0.4f)
)

CompositionLocalProvider(LocalTextSelectionColors provides customTextSelectionColors) {
    SelectionContainer {
        Text(
            text = coin.description,
            style = MaterialTheme.typography.body2
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

PS 问题的一部分是从这个 SO 答案复制的 -链接