1.通用的設(shè)置App語言的方法(需要注意版本的變化)
public static Context initAppLanguage(Context context, String language) {
if (currentLanguageIsSimpleChinese(context)) {
Log.e("LanguageUtil", "當(dāng)前是簡體中文");
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);
}
/**
* 判斷當(dāng)前的語言是否是簡體中文
*
* @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中的語言相當(dāng)于是App的全局設(shè)置战秋。
- Android7.0及以后版本绣硝,使用了LocaleList爆雹。Configuration中的語言設(shè)置可能獲取的不同,而是生效于各自的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", "從后臺進(jìn)入前臺");
isRunInBackground = false;
//防止應(yīng)用程序切換到后臺,然后通過設(shè)置設(shè)置語言市埋,最后再將應(yīng)用程序從后臺切換回前臺的情況
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", "進(jìn)入了后臺");
}
}
@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
如果問題黎泣,歡迎指教。