Java Android本地化
以下代码用于将应用程序区域设置更改为西班牙语,在某些设备中效果良好,但在某些设备中,它会放大(缩放)应用程序中的视图。有人对解决方案有什么看法吗Java Android本地化,java,android,locale,Java,Android,Locale,以下代码用于将应用程序区域设置更改为西班牙语,在某些设备中效果良好,但在某些设备中,它会放大(缩放)应用程序中的视图。有人对解决方案有什么看法吗 Configuration config = getResources().getConfiguration(); // change this to a different Locale than your device Locale locale = new Locale("es", "es_ES"); config.locale = loca
Configuration config = getResources().getConfiguration();
// change this to a different Locale than your device
Locale locale = new Locale("es", "es_ES");
config.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());
Log.i("onSelected..", Locale.getDefault().getCountry());
startActivity(new Intent(getApplicationContext(), HomePage.class));
finish();
//您正在使用displaymetris更新配置 因此,它将对您的配置进行更改
getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());
当我必须使用不同的语言时,我使用此方法: 1) 为所有支持的语言设置int。 2) 使用基本函数设置默认语言环境。 3) 使用一个函数以不同的语言启动 例如: (二) 区域设置跟随 (一) (三)
然后,调用launchApplication(选中int);而且一定是工作 并且您必须将“区域设置”添加到清单中活动的配置更改中。如果没有此选项,我的活动有时会忽略区域设置的更改
public static void setDefaultLocale(Context context,String locale)
{
Locale appLoc = new Locale(locale);
Locale.setDefault(appLoc);
Configuration appConfig = new Configuration();
appConfig.locale = appLoc;
context.getResources().updateConfiguration(appConfig, context.getResources()
.getDisplayMetrics());
}
private Language myLanguage;
public enum Language
{
Null,Spanish,English,Catalan
}
private void launchApplication(int language)
{
// Set Language
switch (language)
{
case 1:
// Español
setDefaultLocale(getApplicationContext(),"es");
myLanguage = Language.Spanish;
break;
case 2:
// English
setDefaultLocale(getApplicationContext(),"en");
myLanguage = Language.English;
break;
default:
// Catalan
setDefaultLocale(getApplicationContext(),"ca");
myLanguage = Language.Catalan;
break;
}
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, 2);
// Finish the Activity when return from the other Activity
finish();
}