Geo*_*Com 5 java android locale localization deprecated
我尝试使用以下菜单资源更改应用语言:
@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);
}
Run Code Online (Sandbox Code Playgroud)
同时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;
Run Code Online (Sandbox Code Playgroud)
我应该如何将这种新方法应用于我的代码!?
| 归档时间: |
|
| 查看次数: |
485 次 |
| 最近记录: |