java - 应用程序语言更改默认为原始语言
问题描述
我已经尝试在 API 27 上对此进行测试,并且效果很好,但是当我切换到 API 21 时,它不会改变语言。
private void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
//config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
config.setLocale(locale);
} else {
config.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
getBaseContext().createConfigurationContext(config);
} else {
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
//getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();
}
public void loadlocale(){
SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
String language = prefs.getString("My_Lang", "");
setLocale(language);
}
我在 onCreate 中的 setContentView 之前调用“loadlocal()”。
在我执行的 onClickListener 中
....
setLocale("en");
recreate();
//or
setLocale("ar");
recreate();
....
解决方案
推荐阅读
- java - 乘法表,询问直到所有正确答案
- javascript - 使用箭头键将单击事件设置为容器中的下一个选项卡
- python-3.x - 获取 pathlib 对象的字符串表示
- ide - WebStorm 中的语法高亮
- go - 如何从 Visual Studio Code 中的包覆盖范围中排除生成的模拟
- javascript - 遍历 JavaScript 中每个列表项的数组
- css - CSS网格中的Flexbox:如何定位弹性项目
- c# - 如何在 WPF DataGrid 中获取 SelectedCells 的“锚”单元格?
- jquery - jQuery 追加/前置在 Edge 中不起作用?
- javascript - forEach 回调中第三个参数(数组)的用途是什么?