Android 我怎样才能在不改变手机语言的情况下改变fragment的语言?
我注意到,我的应用程序只有在我更改手机语言时才会更改语言,但只有对于片段,对于活动,它才会起作用 我正在使用Android 我怎样才能在不改变手机语言的情况下改变fragment的语言?,android,android-fragments,fragment,locale,Android,Android Fragments,Fragment,Locale,我注意到,我的应用程序只有在我更改手机语言时才会更改语言,但只有对于片段,对于活动,它才会起作用 我正在使用 private void setLocale(String lang) { Locale locale= new Locale(lang); Locale.setDefault(locale); Configuration config=new Configuration(); config.locale=locale;
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);
}
及
因此,当我运行应用程序时,我得到了一个alertDialog
来选择语言,选择后,它被更改为唯一的不活动,但不是在一个片段中,我有6个片段
我该如何解决这个问题?你能展示你的fragment类,并确保你在fragmentwhat fragment中有setLocale方法吗?另外,
updateConfiguration
已被弃用method@ArunAbimanyu很抱歉,我没有将setLocale方法放在片段中,我只是将它放在ifrst屏幕中一次,然后在用户从alertDialog中选择语言后,打开一个意图,这是一个spalsh屏幕,然后在4秒钟后,它打开片段,只有底部菜单的语言会因为它的活动而改变,而不是因为fragments@GianhTran很抱歉这么晚,是的,我知道updateConfiguration已经被弃用了,但是,我遵循了youtube上的一个教程,他说即使如此,它仍然可以工作
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();
}