Android语言的动态变化

时间:2012-04-13 06:24:03

标签: android

我有一个Android应用程序作为设备的UI工作。我的意思是,当我打开设备时,我没有看到默认的android界面,而不是我看到我的应用程序。当我改变Android语言时,android的os字符串会改变但我的应用程序字符串不会改变。它需要重新启动。重启后它会改变,但这不是我想要的。因为,该应用程序是我的主应用程序,我必须重新启动所有系统才能重新启动应用程序。 如您所知,此应用程序适用于所有系统生命周期。所以我需要它,在它运行时改变它的语言,而不重启。

我该怎么做?

请注意:Locale不适用于我想要的东西。

2 个答案:

答案 0 :(得分:1)

onCreate 方法中调用以下内容,以选择语言环境字符串中提供的语言。

/**
 * Set the default Locale for app
 * @param context context on which the locale will be implemented
 * @param locale new locale for example, <b>sv</b> for Swedish or <b>en</b> for English
 */
public static void setDefaultLocale(Context context, String locale) {
    Locale locJa = new Locale(locale.trim());
    Locale.setDefault(locJa);

    Configuration config = new Configuration();
    config.locale = locJa;

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

    locJa = null;
    config = null;
}

答案 1 :(得分:1)

您可以使用此功能更改语言..

  public void changeLang(String lang) {
    if (lang.equalsIgnoreCase(""))
        return;


    String languageToLoad  =lang; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
    DebugLog.d("Selectedlang"+lang);


}