我在 Android 12 以上版本中遇到问题,使用 AppCompatDelegate 设置繁体中文。
其余语言运行良好。
这是代码,
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags("zh-Hant")
// Call this on the main thread as it may require Activity.restart()
AppCompatDelegate.setApplicationLocales(appLocale)
Locale.xml:
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="en"/>
<locale android:name="ms"/>
<locale android:name="th"/>
<locale android:name="zh"/>
<locale android:name="zh-Hant-TW"/>
<locale android:name="hi"/>
</locale-config>
构建.gradle-
resConfigs("en", "hi", "ms", "th","zh", "zh-rTW", "b+zh+Hant+TW")
GitHub 参考链接 - https://github.com/android/user-interface-samples/tree/main/PerAppLanguages/views_app
谢谢!
问题是 - 使用更新的 contextWrapper 覆盖我的 AttachBaseContext 方法。 所以,我删除了 AttachBaseContext 方法,它工作正常。
确保您的 Activity 通过 AppCompactActivity 进行扩展,以实现向后兼容的 API,这些 API 与 AppCompatActivity 上下文(而不是应用程序上下文)配合使用,适用于 Android 12(API 级别 32)及更早版本。使用 Appcompat 1.6.0 或更高版本访问向后兼容的 API。