1. 程式人生 > >android -語言設定

android -語言設定

在android中語言適配 響應國際化,一種場景是手動進行語言切換 ,另一種是根據定位得到國家碼匹配。

 這裡僅對中英文切換配置:

  1.在res檔案下建立對應的values-en資料夾 和 values-zh資料夾 建立所屬strings.xml;

   如: values-en下strings.xml  中<string name="hello_world">hello word</string>

   :values-zh下string.xml  中<string

name="hello_world">您好,世界</string>

2.更新Configuration中的locale屬性:

public static  void resetLanguage(int language){
//        int language = SPHelper.getInt(SpKeys.SP_KEY_LANGUAGE);

        //根據讀取到的資料,進行設定
        Resources resources = App.get().getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
Configuration configuration = resources.getConfiguration(); switch (language){ case 0: configuration.setLocale( Locale.CHINESE); break; case 1: configuration.setLocale(Locale.ENGLISH); break; default
: configuration.setLocale(Locale.getDefault()); break; } resources.updateConfiguration(configuration,displayMetrics); // restart 重新啟動首頁 App.reStart(); }