我的应用程序需要支持多语言。我想从应用程序的所有活动中更改应用程序语言。所以我使用Base_Activity并将微调器放在操作栏中。问题是在这里改变语言。当我在微调器中触发某个项来更改语言时,它不会改变。但在触发该项目并转到下一个活动后,它会在那里更改。我试图在每次触发获取触发语言后刷新页面。当选择了微调器中的语言项时,如何使用另一个语言刷新页面。我的代码看起来像这样;
public class Base_Activity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final Spinner spinner = (Spinner) menu.getItem(0).getActionView()
.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.languages, R.layout.spinner_row);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// private String currentLanguage;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// currentLanguage = spinner.getSelectedItem().toString();
Locale locale = null;
switch (arg2) {
case 0:
locale = new Locale("en_US");
break;
case 1:
locale = new Locale("fi");
break;
case 2:
locale = new Locale("fr");
break;
case 3:
locale = new Locale("de");
break;
case 4:
locale = new Locale("sk");
break;
case 5:
locale = new Locale("pl");
break;
default:
locale = new Locale("en_US");
break;
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
startActivity();
}
private void startActivity() {
overridePendingTransition(0, 0);
Log.i("DEBUG", "GATEWAY");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Locale.setDefault(Locale.ENGLISH);
}
});
return super.onCreateOptionsMenu(menu);
}
}
答案 0 :(得分:0)
Locale localeNew = new Locale(locale);
Locale.setDefault(localeNew);
Configuration configNew = new Configuration();
configNew.locale = localeNew;
getBaseContext().getResources().updateConfiguration(configNew, null);