使用以下代码是用于把应用语言环境转换成汉语,程序在一些设备上能正常的运行,但是在另外一些设备上,它把程序中的视图都放大了。这是为什么呢?谁能给点建议啊?
Configuration config = getResources().getConfiguration();
Locale locale = new Locale("es", "es_ES"); // change this to a different Locale than your device
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();
当我使用不同的语言时,我使用以下方法
1)给所有支持的语言设置int型
private Language myLanguage;
public enum Language
{
Null,Spanish,English,Chinese
}
2)使用基本功能设置Default Locale
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());
}
3)使用一个函数来发布不同的语言
private void launchApplication(int 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:
// Chinese
setDefaultLocale(getApplicationContext(),"ca");
myLanguage = Language.Chinese;
break;
}
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, 2);
// 当从别的Activity返回时,结束当前的Activity
finish();
}
然后调用launchApplication。
你使用displaymetris更新configuration
所以应该改变configuration
getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());
你在使用displymetris更新配置。所以它将改变您的配置:
getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());