依赖版本:androidx.appcompat:appcompat:1.1.0,BaseActivity中实现下面方法:
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
// 兼容androidX在部分手机切换语言失败问题
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
依赖版本:androidx.appcompat:appcompat:1.2.0时,BaseActivity中实现下面方法:
@Override
protected void attachBaseContext(Context newBase) {
if (shouldSupportMultiLanguage()) {
Context context = LanguageUtil.attachBaseContext(newBase);
final Configuration configuration = context.getResources().getConfiguration();
// 此处的ContextThemeWrapper是androidx.appcompat.view包下的
// 你也可以使用android.view.ContextThemeWrapper,但是使用该对象最低只兼容到API 17
// 所以使用 androidx.appcompat.view.ContextThemeWrapper省心
final ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context,
R.style.Theme_AppCompat_Empty) {
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
overrideConfiguration.setTo(configuration);
}
super.applyOverrideConfiguration(overrideConfiguration);
}
};
super.attachBaseContext(wrappedContext);
} else {
super.attachBaseContext(newBase);
}
}
附LanguageUtil
/**
* Created by zlx on 2020/11/5 17:10
* Email: 1170762202@qq.com
* Description:
*/
public class LanguageUtil {
private static final String TAG = "LanguageUtil";
public static String getSystemLanguage() {
String language = Locale.getDefault().getLanguage();
String country = Locale.getDefault().getCountry();
return language + "-" + country;
}
public static String getCurrentLanguage() {
String language = OtherSp.getInstance().getLanguage();
if (TextUtils.isEmpty(language)) {
language = Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
}
// LogUtils.e("getCurrentLanguage", language);
return language;
}
public static boolean isChinese() {
String currentLanguage = getCurrentLanguage();
String s = currentLanguage.split("-")[0].toLowerCase();
if (s.equals("zh")) {
LogUtils.eTag("isChinese", true);
return true;
}
LogUtils.eTag("isChinese", false);
return false;
}
public static void switchLanguage() {
String language = OtherSp.getInstance().getLanguage();
if (TextUtils.isEmpty(language)) {
if (Locale.getDefault().equals(Locale.SIMPLIFIED_CHINESE)) {
language = Locale.US.getLanguage() + "-" + Locale.US.getCountry();
} else {
language = Locale.SIMPLIFIED_CHINESE.getLanguage() + "-" + Locale.SIMPLIFIED_CHINESE.getCountry();
}
} else if (language.equals(Locale.SIMPLIFIED_CHINESE.getLanguage() + "-" + Locale.SIMPLIFIED_CHINESE.getCountry())) {
language = Locale.US.getLanguage() + "-" + Locale.US.getCountry();
} else {
language = Locale.SIMPLIFIED_CHINESE.getLanguage() + "-" + Locale.SIMPLIFIED_CHINESE.getCountry();
}
changeLanguage(language);
}
private static void changeLanguage(String language) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
LanguageUtil.setConfiguration(CommonApplication.getMyApplication(), language);
}
OtherSp.getInstance().saveLanguage(language);
HeaderHelper.getInstance().setLang();
// SpUtil.getInstance(this).putString(SpUtil.LANGUAGE, language);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static Locale getLocaleByLanguage(String language) {
Locale locale = Locale.getDefault();
if (TextUtils.isEmpty(language)) {
return locale;
}
if (language.equals(Locale.SIMPLIFIED_CHINESE.getLanguage() + "-" + Locale.SIMPLIFIED_CHINESE.getCountry())) {
locale = Locale.SIMPLIFIED_CHINESE;
} else if (language.equals(Locale.US.getLanguage() + "-" + Locale.US.getCountry())) {
locale = Locale.US;
}
Log.d(TAG, "getLocaleByLanguage: " + locale.getDisplayName());
return locale;
}
public static Context attachBaseContext(Context context) {
String language = LanguageUtil.getCurrentLanguage();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
} else {
return context;
}
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Resources resources = context.getResources();
Locale locale = LanguageUtil.getLocaleByLanguage(language);
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
return context.createConfigurationContext(configuration);
}
/**
* @param context
* @param newLanguage 想要切换的语言类型 比如 "en" ,"zh"
*/
@SuppressLint("NewApi")
public static void setConfiguration(Context context, String newLanguage) {
if (TextUtils.isEmpty(newLanguage)) {
return;
}
Locale locale = getLocaleByLanguage(newLanguage);
//获取应用程序的所有资源对象
Resources resources = context.getResources();
//获取设置对象
Configuration configuration = resources.getConfiguration();
//如果API < 17
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.locale = locale;
} else //如果 17 < = API < 25 Android 7.7.1
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
configuration.setLocale(locale);
} else {//API 25 Android 7.7.1
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
}
DisplayMetrics dm = resources.getDisplayMetrics();
resources.updateConfiguration(configuration, dm);
}
}