我尝试使用以下菜单资源更改应用语言:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.eng:
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale; // deprecated!!
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics()); // deprecated!!
this.setContentView(R.layout.activity_main);
break;
case R.id.de:
languageToLoad = "de";
locale = new Locale(languageToLoad);
Locale.setDefault(locale);
config = new Configuration();
config.locale = locale; // deprecated!!
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics()); // deprecated!!
this.setContentView(R.layout.activity_main);
break;
}
return super.onOptionsItemSelected(item);
}
compileSdkVersion 25
,minSdkVersion 16
,targetSdkVersion 25
我知道更换是使用createConfigurationContext
,但我不知道如何使用它。
我试过这个,但它没有用:
switch (item.getItemId()) {
case R.id.eng:
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration overrideConfiguration = getBaseContext().getResources().getConfiguration();
overrideConfiguration.setLocale(locale);
Context context = createConfigurationContext(overrideConfiguration);
Resources resources = context.getResources();
this.setContentView(R.layout.activity_main);
break;
我应该如何将这种新方法应用到我的代码中??