我从上周开始尝试这一切。我试图更改微调器活动的语言。虽然我可以更改语言,但是一旦我更改语言 MainActivity 会继续刷新。我的观点是 refresh() 函数有问题。任何指针或我做错了什么。这是我的函数:-
public boolean onNavigationItemSelected(int position, long id) {
if(position == 0){
setLocale("fr");
Toast.makeText(MainActivity.this, "Locale in French !", Toast.LENGTH_LONG).show();
}
if(position == 1){
setLocale("es");
Toast.makeText(MainActivity.this, "Locale in Spain !", Toast.LENGTH_LONG).show();
}
if(position == 2){
setLocale("en");
Toast.makeText(MainActivity.this, "Locale in English !", Toast.LENGTH_LONG).show();
}
return true;
}
public void setLocale(String localeCode){
Locale locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
refresh();
//Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();
}
private void refresh() {
finish();
//setContentView(R.layout.activity_main);
Intent myIntent = new Intent(this, MainActivity.class);
// Intent myIntent = new Intent(this, OpertingSystemFragment.class);
startActivity(myIntent);
}