我正在尝试在我的应用程序中使用阿拉伯语和英语。它在运行 Android Nougat 或更低版本的设备上运行良好。但它不适用于奥利奥设备。 API 26 中有一些新的代码要求吗?我正在使用下面的代码。
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
我将“en”和“ar”作为语言参数传递。
当你设置新的Locale
你应该重新启动你的Activity
。您可以使用下一个代码片段来执行它:
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
那么你的changeLanguage()
方法将以如下方式查看:
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
restartActivity();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)