【问题标题】:Android - Locale ChangerAndroid - 语言环境更改器
【发布时间】:2019-04-04 17:35:52
【问题描述】:

我在更改 Locale 时遇到了奇怪的行为。每个Android API levels 都运行良好。 试图更改我的应用程序的Locale,但它不再工作了。 设置新的Locale -> AttachBaseContext -> Recreate Activity 时似乎一切正常,但最后我只看到来自English 的字符串。

你有什么建议,或者最新的Android Studio 3.5 Canary 9可能有问题?

这是我的自定义上下文包装器:

class MyContextWrapper(base: Context) : ContextWrapper(base) {
    companion object {
        @TargetApi(Build.VERSION_CODES.N)
        fun wrap(contextt: Context, newLocale: Locale): ContextWrapper {
            var context = contextt
            val res = context.resources
            val configuration = res.configuration
            when {
                VersionUtils.isAfter24 -> {
                    configuration.setLocale(newLocale)
                    val localeList = LocaleList(newLocale)
                    LocaleList.setDefault(localeList)
                    configuration.locales = localeList
                    context = context.createConfigurationContext(configuration)
                }
                VersionUtils.isAfter17 -> {
                    configuration.setLocale(newLocale)
                    context = context.createConfigurationContext(configuration)
                }
                else -> {
                    @Suppress("DEPRECATION")
                    configuration.locale = newLocale
                    @Suppress("DEPRECATION")
                    res.updateConfiguration(configuration, res.displayMetrics)
                }
            }
            return ContextWrapper(context)
        }
    }
}

【问题讨论】:

标签: java android kotlin


【解决方案1】:

这是AppCompat 1.1.0-alpha03的一个bug,终于用最新的alphaAppCompat 1.1.0-alpha04解决了。

我花了 3 天半的时间才明白出了什么问题,因为在注意到这种行为之前进行了多次重构。

当你是early alpha tester时会发生这种情况!

作为建议,因为这发生在我之前(不同的错误),请确保在继续改进和重构应用程序之前测试您在更新到另一个 alpha 时使用的所有功能。 :) :)

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多