为什么 Jetpack Compose 预览显示错误的字符串资源?

问题描述 投票:0回答:1

有时,当我在可组合项中使用字符串资源时,预览会显示错误的字符串。它对于文字字符串总是工作得很好,只有字符串资源是错误的。该错误不一致。

例如,如果我有这个 strings.xml:

<resources>
    <string name="app_name">Violit</string>
    <string name="load_topic_failure_message">Something went wrong loading the topic</string>
</resources>

我有这个可组合项:

@Composable
fun TopicFailureContent() {
    Text(stringResource(R.string.load_topic_failure_message))
}

@Preview(showBackground = true)
@Composable
fun TopicFailureContentPreview() {
    TopicFailureContent()
}

它可能会呈现“部分检查”或“导航菜单”之类的内容,而不是“加载主题时出错”。

如果我将可组合项更改为:

@Composable
fun TopicFailureContent() {
    Text("Something went wrong loading the topic")
}

它可以正确渲染预览。

看起来预览可能会渲染附近的字符串,而不是我想要的字符串。字符串资源在测试和运行应用程序中工作正常。这只是预览,并不总是有效。

我正在使用 Android Studio Electric Eel 2022.1.1,但我在之前的版本上也遇到了同样的问题。这在 Compose UI 版本 1.2.1 和 1.3.3 上均失败。

知道为什么字符串资源在预览中并不总是有效以及如何修复它吗?

android kotlin android-jetpack-compose android-resources android-jetpack-compose-preview
1个回答
0
投票

编辑资源字符串文件后会出现此问题。 它通过构建项目并在每次编辑资源文件后重新启动 IDE 来解决我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.