Android国际化适配包含内容:
1.图片国际化适配图片适配
2.文字国际化适配文字适配
原文转载自:https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
由于从网上搜了很多文字化国家化适配的文章,感觉写的都不是很全面而且在8.0上测试总是有问题,所以经过一番研究就把实现的方法整理出来,分享一下,本文主要是讲代码实现。(方便自己以后之际copy来用)
多语言文字切换实现
1.定义一个工具类,内容如下:
'''
public class LocaleManager {
public static final String LANGUAGE_ENGLISH = "en";
public static final String LANGUAGE_CHINESE = "zh";
private static final String LANGUAGE_KEY = "language_key";
public static Context setLocale(Context c) {
return updateResources(c, getLanguage(c));
}
//根据手机版本号进行设置语言环境
public static Context setNewLocale(Context c, String language) {
persistLanguage(c, language);
return updateResources(c, language);
}
//SharedPreferences获取语言环境
public static String getLanguage(Context c) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
return prefs.getString(LANGUAGE_KEY, LANGUAGE_ENGLISH);
}
@SuppressLint("ApplySharedPref")
private static void persistLanguage(Context c, String language) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
// use commit() instead of apply(), because sometimes we kill the application process immediately
// which will prevent apply() to finish
prefs.edit().putString(LANGUAGE_KEY, language).commit();
}
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
//做版本兼容性判断
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
//点进去看方法详情
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
public static Locale getLocale(Resources res) {
Configuration config = res.getConfiguration();
return Build.VERSION.SDK_INT >= 24 ? config.getLocales().get(0) : config.locale;
}
}
'''
主要解决:updateConfiguration 方法过期的问题,但是这样依旧很难再APP中把所有文字修改,因为Android26等高版本需要在Acivity中做如下操作:
BaseActivity
'''
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.setLocale(base));
Log.d(TAG, "attachBaseContext");
}
}
'''
同时在Application中做如下设置:
'''
public class App extends Application {
private final String TAG = "App";
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.setLocale(base));
Log.d(TAG, "attachBaseContext");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleManager.setLocale(this);
Log.d(TAG, "onConfigurationChanged: " + newConfig.locale.getLanguage());
}
}
'''
补充:
'''
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvChinese:
LocaleManager.setNewLocale(this, LocaleManager.LANGUAGE_CHINESE);
break;
case R.id.tvEnglish:
LocaleManager.setNewLocale(this, LocaleManager.LANGUAGE_ENGLISH);
break;
}
Intent i = new Intent(this, MainActivity.class);
startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
}
'''
经过测试就可以正常在app中切换语言了具体细节在下一篇博客中做详细讲解。