话不多说,先上个工具类:
package com.adorone.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import java.util.Locale;
/**
* Created by Bruce on 2021/1/9
*/
public class SpUtil {
private static final StringSP_NAME ="poemTripSpref";
private static SpUtilspUtil;
private static SharedPreferenceshmSpref;
private static SharedPreferences.Editoreditor;
private SpUtil(Context context) {
hmSpref = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
editor =hmSpref.edit();
}
public static SpUtilgetInstance(Context context) {
if (spUtil ==null) {
synchronized (SpUtil.class) {
if (spUtil ==null) {
spUtil =new SpUtil(context);
}
}
}
return spUtil;
}
public void putString(String key, String value) {
editor.putString(key, value);
editor.commit();
}
public StringgetString(String key) {
return hmSpref.getString(key, "");
}
public static ContextattachBaseContext(Context context, String language) {
// Log.d(TAG, "attachBaseContext: " + Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}else {
return context;
}
}
private static ContextupdateResources(Context context, String language) {
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
Locale locale;
// 繁体判断
if (language.equals("rTW")){
locale =new Locale("zh","TW");
}else {
locale =new Locale(language);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList defaultList = LocaleList.forLanguageTags(language);
LocaleList.setDefault(defaultList);
conf.setLocales(defaultList);
}else {
conf.locale = locale;
}
conf.locale = locale;
conf.setLocale(locale);
context = context.createConfigurationContext(conf);
res.updateConfiguration(conf, dm);
return context;
}
}
注意:繁体切换看你们设置的简写或者获取的简写,上面工具类是我自己设置。
公司要出个多国语言切换,本来想着就切换一两国就行了,其他的语言自己跟随系统嘛,但是万恶的产品,28国语言全部要可以应用内切换,当时脑海里就飞过了一万只那个马,但是能怎么办呢?两百多斤的产品我又打不过,只能先百度,google搜索一下,然后也找了一些demo测试,不行啊,28国的需求满足不了,Locale.SIMPLIFIED_CHINESE,只有几国语言,最后还是要自己去看Locale的源码
带country 参数的就是解决繁体的关键,处设置语言code,还需要指明国家,但是台湾也是中国的,但是他源码这么设置了没办法,只能跟着做,但是不会改变我的初心。
然后就是设置语言,当你选择了需要切换的语言,跳转MainActivity
//存储我的语言code
SPUtils.putString(LanguageSettingActivity.this, "LANGUAGE2", languageNew);
Intent intent =new Intent(LanguageSettingActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
SpUtil.attachBaseContext(AppApplication.getInstance(),languageNew);
languageNew就是你新设置的语言code。
还有,需要在BaseActivity中重写
@Overrideprotected void attachBaseContext(Context newBase) { //获取我们存储的语言环境 比如 "en","zh",等等 String language = SPUtils.getString(AppApplication.getInstance(),"LANGUAGE2","en"); //attach 对应语言环境下的context super.attachBaseContext(SpUtil.attachBaseContext(newBase, language));
}
乐在分享!