2012-04-13 65 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); 


}