1.通用的设置App语言的方法(需要注意版本的变化)
public static Context initAppLanguage(Context context, String language) {
if (currentLanguageIsSimpleChinese(context)) {
Log.e("LanguageUtil", "当前是简体中文");
return context;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//7.0及以上的方法
Log.e("LanguageUtil", "7.0及以上");
return createConfiguration(context, language);
} else {
Log.e("LanguageUtil", "7.0以下");
updateConfiguration(context, language);
return context;
}
}
/**
* 7.0及以上的修改app语言的方法
*
* @param context context
* @param language language
* @return context
*/
@RequiresApi(api = Build.VERSION_CODES.N)
private static Context createConfiguration(Context context, String language) {
Resources resources = context.getResources();
Locale locale = new Locale(language);
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
return context.createConfigurationContext(configuration);
}
/**
* 7.0以下的修改app语言的方法
*
* @param context context
* @param language language
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private static void updateConfiguration(Context context, String language) {
Resources resources = context.getResources();
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
resources.updateConfiguration(configuration, displayMetrics);
}
/**
* 判断当前的语言是否是简体中文
*
* @param context context
* @return boolean
*/
private static boolean currentLanguageIsSimpleChinese(Context context) {
String language;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
language = context.getResources().getConfiguration().getLocales().get(0).getLanguage();
} else {
language = context.getResources().getConfiguration().locale.getLanguage();
}
Log.e("LanguageUtil", "language = " + language);
return TextUtils.equals("zh", language);
}
2.初始化的位置
- Android7.0及以前版本,Configuration中的语言相当于是App的全局设置。
- Android7.0及以后版本,使用了LocaleList。Configuration中的语言设置可能获取的不同,而是生效于各自的Context。也就是语言需要植入到Context中,每个Context都植入一遍。
7.0及以前的版本,在Application中的初始化(注意在项目清单文件中注册applicaiton)
public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {
private boolean isRunInBackground;
private int appCount;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
initLanguage();
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
appCount++;
if (isRunInBackground) {
Log.e("TAG", "从后台进入前台");
isRunInBackground = false;
//防止应用程序切换到后台,然后通过设置设置语言,最后再将应用程序从后台切换回前台的情况
initLanguage();
}
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
appCount--;
if (appCount == 0) {
isRunInBackground = true;
Log.e("TAG", "进入了后台");
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
private void initLanguage() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
//Application这种方式适用于8.0之前(不包括8.0)的版本
LanguageUtil.initAppLanguage(getApplicationContext(), "zh");
}
}
8.0及之后的版本(在基类BaseActivity中重写attachBaseContext(Context newBase)方法)
public class BaseActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
super.attachBaseContext(newBase);
} else {
//zh:中文
super.attachBaseContext(LanguageUtil.initAppLanguage(newBase, "zh"));
}
}
}
Github地址:https://github.com/lucklyperson/LanguageDemo
如果问题,欢迎指教。