2012-04-13 68 views
0

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

我该怎么做?

请注意:语言环境不适合我想要的东西。

回答

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; 
} 
+0

谢谢你,但它没有工作,你确定它是否正在工作?也许我做错了什么。 – juliadream 2012-04-13 06:54:06

+0

是的,它的工作原理。在** setContentView ** – waqaslam 2012-04-13 06:56:14

+0

之前调用它好吧,我会再试一次。我的结构有点不同,没有onCreate或setContentView方法,所以我认为我找不到调用此函数的正确位置。 – juliadream 2012-04-13 06:59:45

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


}