You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否修改Compose默认Preview Composable的语言?附主题预览代码

可以直接修改Preview Composable的语言

无需依赖虚拟设备或IDE全局设置,你可以在Preview Composable内部通过覆盖LocalConfiguration来指定语言。

具体实现示例:

@Preview
@Composable
fun AppPreviewChinese() {
    val configuration = LocalConfiguration.current.apply {
        setLocale(Locale.SIMPLIFIED_CHINESE)
    }
    CompositionLocalProvider(LocalConfiguration provides configuration) {
        AppBookTheme(darkTheme = false) {
            TheBookApp()
        }
    }
}

@Preview
@Composable
fun AppDarkThemePreviewChinese() {
    val configuration = LocalConfiguration.current.apply {
        setLocale(Locale.SIMPLIFIED_CHINESE)
    }
    CompositionLocalProvider(LocalConfiguration provides configuration) {
        AppBookTheme(darkTheme = true) {
            TheBookApp()
        }
    }
}

通过CompositionLocalProvider替换当前的配置,就能让Preview直接使用指定的语言渲染界面,不需要修改外部设置。

内容的提问来源于stack exchange,提问作者Dimitri LE MOING

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:45:35