Android 9。
这是我切换应用程序语言的代码:
public static void changeLanguageApp(Context context, Locale locale) {
Resources res = context.getResources();
Configuration config = res.getConfiguration();
Locale currLocale = getLocale(config);
Configuration conf;
if (currLocale != locale) {
Locale.setDefault(locale);
conf = new Configuration(config);
conf.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
当应用程序运行时,开关效果很好。但是,如果我关闭应用程序然后运行它,则有设备的语言,而不是应用程序的语言。当我运行应用程序时,我会使用此代码:
public void onCreate() {
super.onCreate();
loadResources(this);
}
public static Context loadResources(Context context) {
return changeLocale(context, this, getResources().getConfiguration().locale);
}
public static Context changeLocale(Context context, Locale locale) {
Locale currentLocation = new Locale(locale.getLanguage());
Locale.setDefault(currentLocation);
Resources res = context.getResources();
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(currentLocation);
context = context.createConfigurationContext(config);
res.updateConfiguration(config, res.getDisplayMetrics());
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
在调试中,我会看到,该应用程序语言检测到了正确的,但是我不明白,为什么不设置应用程序。 这种情况在Android 9和一些Android 11设备上。
This is my code for switching app language:
public static void changeLanguageApp(Context context, Locale locale) {
Resources res = context.getResources();
Configuration config = res.getConfiguration();
Locale currLocale = getLocale(config);
Configuration conf;
if (currLocale != locale) {
Locale.setDefault(locale);
conf = new Configuration(config);
conf.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
when app is running switching works good. But if I close app and then run it, there are language of device, not of app. When I run my app I use this code:
public void onCreate() {
super.onCreate();
loadResources(this);
}
public static Context loadResources(Context context) {
return changeLocale(context, this, getResources().getConfiguration().locale);
}
public static Context changeLocale(Context context, Locale locale) {
Locale currentLocation = new Locale(locale.getLanguage());
Locale.setDefault(currentLocation);
Resources res = context.getResources();
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(currentLocation);
context = context.createConfigurationContext(config);
res.updateConfiguration(config, res.getDisplayMetrics());
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
In debug I see, that app language detected right, but I dont understend, why doesn't it set for app.
Such situation is on Android 9 and some devices Android 11.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论