小编Pet*_*iff的帖子

更改语言环境在 Android 10 中停止工作

我一直在使用下面的代码来更改 Android 应用程序中的语言环境(该应用程序有自己的语言环境设置,可能与操作系统语言环境不同)。该代码可以正常工作到 Android 9 (P)。在 Android 10 (Q) 中,它停止工作,资源未更新。我在 Android 10 发行说明中没有看到任何与语言环境相关的更改。什么会在 Android 10 中破坏此代码?如果它是已知的,有人可以指出我的解决方案吗?

private fun setLocale(context: Context, language: String): Context {
    //...persist here. persisting works fine
    return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
        updateResources(context, language)
    else
        updateResourcesLegacy(context, language)
}

@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)

    val configuration = context.resources.configuration
    configuration.setLocale(locale)
    configuration.setLayoutDirection(locale)

    return context.createConfigurationContext(configuration)
}
Run Code Online (Sandbox Code Playgroud)

更新

我发现此代码在升级到较新版本的androidx.appcompat:appcompat. 我可以缩小范围:它1.2.0-alpha011.2.0-alpha02.

我在发行说明中1.2.0-alpha02看到有 3 个与上下文相关的更改:https : …

android android-architecture-components android-jetpack android-10.0

10
推荐指数
1
解决办法
1052
查看次数