Android本地化

sri*_*tty 12 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 = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());
Log.i("onSelected..", Locale.getDefault().getCountry());
startActivity(new Intent(getApplicationContext(), HomePage.class));
finish();   
Run Code Online (Sandbox Code Playgroud)

vgo*_*anz 14

当我必须使用不同的语言时,我使用这种方法:

1)为所有支持的语言设置一个int.2)使用基本功能设置默认区域设置.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());
}
Run Code Online (Sandbox Code Playgroud)

locale遵循ISO 639-1

1)

private Language myLanguage;
public enum Language 
{
    Null,Spanish,English,Catalan
}
Run Code Online (Sandbox Code Playgroud)

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


}
Run Code Online (Sandbox Code Playgroud)

然后,调用launchApplication(int selected); 一定要工作!

  • @srinivasa rao ramisetty ...然后将答案标记为已接受.这会对很多人有所帮助. (3认同)

小智 6

并且您必须为清单中的活动的配置更改添加"区域设置".没有这个,我的活动有时会忽略区域设置更改


Pad*_*mar 3

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

所以它会改变你的配置

getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)