2016-04-28 87 views
3

我想編寫一個可以顯示英文或中文的應用程序。我已經準備了2個string.xml,它是value/strings.xml和value-zh-rHK/strings.xml。但我不知道如何通過Android的ListPreference更改語言。如何更改整個android應用程序的語言?

XML /的preferences.xml:

<?xml version="1.0" encoding="utf-8"?> 

<SwitchPreference 
    android:key="pref_nightmode" 
    android:title="@string/nightmode" 
    android:defaultValue="false"> 
</SwitchPreference> 

<ListPreference 
    android:key="pref_lang" 
    android:title="@string/lang" 
    android:dialogTitle="Choose Language" 
    android:entries="@array/lang" 
    android:entryValues="@array/lang_value" 
    android:defaultValue="@string/lang_default"> 
</ListPreference> 

和Preferences.java

public class Preferences extends BaseActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setTitle(drawer_menu[5]); 
    getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainPreferenceFragment()).commit(); 
} 

public static class MainPreferenceFragment extends PreferenceFragment { 
    String locale; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
     PreferenceManager pm = getPreferenceManager(); 
     ListPreference lang = (ListPreference) pm.findPreference("pref_lang"); 
     if(lang.getValue().equals("English")) { 
      locale = "en_US"; 
     } else { 
      locale = "zh_HK"; 
     } 
    } 
} 

活動延伸BaseActivity因爲我有一個抽屜菜單在那裏。

+0

可以在Android設置中更改系統的語言。 – Robert

+1

我只想更改應用程序的語言,而不是我的電話 – rascee

回答

10

您只能更改應用程序的語言設置。使用Locale類並更新默認配置。語言更改僅在活動重新啓動或重新啓動應用程序時應用。使用下列語言環境類

public class LocaleUtils { 

    private static Locale sLocale; 

    public static void setLocale(Locale locale) { 
     sLocale = locale; 
     if(sLocale != null) { 
      Locale.setDefault(sLocale); 
     } 
    } 

    public static void updateConfig(ContextThemeWrapper wrapper) { 
     if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      Configuration configuration = new Configuration(); 
      configuration.setLocale(sLocale); 
      wrapper.applyOverrideConfiguration(configuration); 
     } 
    } 

    public static void updateConfig(Application app, Configuration configuration) { 
     if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      //Wrapping the configuration to avoid Activity endless loop 
      Configuration config = new Configuration(configuration); 
      config.locale = sLocale; 
      Resources res = app.getBaseContext().getResources(); 
      res.updateConfiguration(config, res.getDisplayMetrics()); 
     } 
    } 
} 

應用類:

public class App extends Application { 
    public void onCreate(){ 
     super.onCreate(); 
     // get user preferred language set locale accordingly new locale(language,country) 
     LocaleUtils.setLocale(new Locale("iw")); 
     LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration()); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     LocaleUtils.updateConfig(this, newConfig); 
    } 
} 

BaseActivity:Changing Locale within the app itself

Android的文檔:http://developer.android.com/reference/java/util/Locale.html

public class BaseActivity extends Activity { 
    public BaseActivity() { 
     LocaleUtils.updateConfig(this); 
    } 
} 

以上回答摘自

+0

那意味着我不需要優先執行此操作?我可以在普通的類中編寫代碼但使用上面的方法? – rascee

+0

您應該使用首選項來保存用戶首選項。根據應用程序類獲取來自首選項的值並更新語言環境。 setLocale後如何刷新視圖 – USKMobility

+0

? – WolfJee

相關問題