安卓多语言切换时底部导航栏语言切换错误
切换的语言不一样,解决办法是在代码中设置string
String[] bottomString = getResources().getStringArray(R.array.bottom_navigation);
for (int i = 0; i < bottomString.length; i++) {
mBottomNavigationView.getMenu().getItem(i).setTitle(bottomString[i]);
}
ok搞定
语言切换
Resources resources = App.getContext().getResources(); Configuration configuration = resources.getConfiguration(); Locale locale = isEn ? Locale.ENGLISH : Locale.CHINA; configuration.setLocale(locale); //安卓8.0以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { configuration.setLocales(new LocaleList( Locale.ENGLISH, Locale.CHINA)); App.getContext().createConfigurationContext(configuration); } else { resources.updateConfiguration(configuration, resources.getDisplayMetrics()); }
安卓8.0以上需要在activity里面的attachContext方法调用
Activity或application中重写这个方法
@Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(AppUtil.attachBaseContext(newBase)); }
AppUtil
public static Context attachBaseContext(Context context) { // 8.0需要使用createConfigurationContext处理 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return updateResources(context, getCurLanguage()); } else { return context; } } @TargetApi(Build.VERSION_CODES.N) private static Context updateResources(Context context, Locale locale) { Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); configuration.setLocale(locale); configuration.setLocales(new LocaleList(locale)); return context.createConfigurationContext(configuration); }