android - 如何在不更改电话语言的情况下更改片段的语言?
问题描述
我注意到我的应用程序仅在我更改电话语言时才更改语言,但仅对于片段,对于活动,它才有效
我在用着
private void setLocale(String lang) {
Locale locale= new Locale(lang);
Locale.setDefault(locale);
Configuration config=new Configuration();
config.locale=locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor=getSharedPreferences("Settings",MODE_PRIVATE).edit();
editor.putString("My_Lang",lang);
editor.commit();
}
public void loadLocale(){
SharedPreferences editor=getSharedPreferences("Settings",Splash.MODE_PRIVATE);
String language=editor.getString("My_Lang","My_Lang");
Log.d("hahahahahahdhjzshsdj",language);
setLocale(language);
}
和
private void showChangeLanguageDialog() {
final String[] listLang={"Francais","العربية","English"};
AlertDialog.Builder onBuilder=new AlertDialog.Builder(Splash.this);
onBuilder.setTitle("Choose Language...");
onBuilder.setSingleChoiceItems(listLang, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==0){
setLocale("fr");
recreate();
Intent intent=new Intent(Splash.this,Splash2.class);
startActivity(intent);
}
else if(which==1){
setLocale("ar");
recreate();
Intent intent=new Intent(Splash.this,Splash2.class);
startActivity(intent);
}
else if(which==2){
setLocale("en");
recreate();
Intent intent=new Intent(Splash.this,Splash2.class);
startActivity(intent);
}
dialog.dismiss();
}
});
AlertDialog mDialog=onBuilder.create();
mDialog.show();
}
所以,当我运行应用程序时,我有一个alertDialog
选择语言,选择它后,它改变了唯一的不活动但不是在一个片段中,我有 6 个片段
我该如何解决?
解决方案
推荐阅读
- nginx - Nginx - 在缓存过期时使用 proxy_cache_lock 逻辑以避免多上行请求
- vb.net - 如何在 Visual Basic 中从 Ms Access 数据库中获取特定列
- elasticsearch - 在 Elasticsearch 中使用搜索模板进行日期范围查询
- typescript - 如何访问基类对象以列出其属性?
- python - 从数组列表Python中获取平均列值
- excel - vba图表X轴
- html - 如何将一条线的 SVG 路径旋转 90 度
- c# - 如何在 C# 中使用 JSON 使用构造函数填充对象属性?
- css - 用渐变绘制气泡反射
- java - 尝试使用函数将数据传递给新控制器会给出 NullPointerException