安卓本地化

12
以下代码是用于将应用程序语言环境更改为西班牙语的,它在某些设备上正常工作,但在某些设备上会放大(缩放)应用程序视图。是否有人有解决方案?
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();   
3个回答

14
我在需要使用不同语言时使用这种方法: 1)为所有支持的语言设置int。 2)使用基本函数设置默认Locale。 3)使用一个函数来启动不同的语言。
以下是示例:
2)
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());
}

当地设置遵循ISO 639-1标准

1)

private Language myLanguage;
public enum Language 
{
    Null,Spanish,English,Catalan
}

3)

    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();


}

然后,调用launchApplication(int selected);并且必须工作!

6

你需要在清单文件中为你的活动添加“locale”配置更改。没有这个配置,我的活动有时会忽略语言环境的更改。


3

//你正在使用displaymetris更新配置

这将会对你的配置进行更改

getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接