/** * @author xt on 2020/1/6 13:09 * 英文增加value-en文件夾 * 中文簡體增加values-zh文件夾 */ public class MyLanguageUtils { public static Context updateLanguage(Context context) { Locale locale = getCurrentLocale(context); if (locale == null) { return context; } Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); DisplayMetrics displayMetrics = resources.getDisplayMetrics(); configuration.setLocale(locale); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { return context.createConfigurationContext(configuration); } else { resources.updateConfiguration(configuration, displayMetrics); return context; } } /** * 默認:0 * 簡體中文:1 * 英文:2 * * @param context * @param locale */ public static void saveLanguage(Context context, Locale locale) { SharedPreferences preferences = context.getSharedPreferences("language", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); int language = 0; if (locale != null) { if (locale.equals(Locale.SIMPLIFIED_CHINESE)) { language = 1; } else if (locale.equals(Locale.ENGLISH)) { language = 2; } } editor.putInt("language", language); editor.commit(); } /** * 中文分簡體和繁體,英文不分是UK、US、CANADA,目前只支持簡體中文和英文 * * @param context * @return */ public static Locale getCurrentLocale(Context context) { Locale locale = getSystemLocale(context); { SharedPreferences preferences = context.getSharedPreferences("language", Context.MODE_PRIVATE); int language = preferences.getInt("language", 0); switch (language) { case 1: locale = Locale.SIMPLIFIED_CHINESE; break; case 2: locale = Locale.ENGLISH; break; default: break; } } { //當返回的語言是不支持的語言時就返回默認語言,當前爲英語 if ((!locale.equals(Locale.SIMPLIFIED_CHINESE) && !locale.equals(Locale.ENGLISH))) { locale = Locale.ENGLISH; } } return locale; } private static Locale getSystemLocale(Context context) { Locale locale; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多語言設置獲取頂部的語言 locale = context.getResources().getConfiguration().getLocales().get(0); } else { locale = context.getResources().getConfiguration().locale; } return locale; } /** * 打開新的activity後,關閉新的activity前面的所有activity * @param context * @param activityClass */ public static void restartApp(Context context, Class activityClass) { Intent intent = new Intent(context, activityClass); context.startActivity(intent); } /** * 是否是簡體中文 * * @param context * @return */ public static boolean isSimplifiedChinese(Context context) { return getCurrentLocale(context).equals(Locale.SIMPLIFIED_CHINESE); } }
切換語言,會造成屏幕方向和大小的api錯亂
BaseActivity中實現方法
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(MyLanguageUtils.updateLanguage(newBase));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getResources().getConfiguration().orientation = newConfig.orientation;
getApplication().getResources().getConfiguration().orientation = newConfig.orientation;
getApplication().getResources().getDisplayMetrics().setTo(getResources().getDisplayMetrics());
}
Application中的實現
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(MyLanguageUtils.updateLanguage(base));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getResources().getConfiguration().orientation = newConfig.orientation;
MyLanguageUtils.updateLanguage(this);
}