以编程方式设置区域设置我的应用程序支持3种(很快是4种)语言。由于几个地区非常相似,我想给用户在我的应用程序中更改语言环境的选项,例如,一个意大利人可能更喜欢西班牙语而不是英语。用户是否可以从应用程序可用的区域设置中选择,然后更改所使用的区域设置?我不认为每个活动设置区域设置是一个问题,因为这是一个在基类中执行的简单任务。
3 回答
红糖糍粑
TA贡献1815条经验 获得超6个赞
Locale locale = new Locale("ru");Locale.setDefault(locale);Configuration config = getBaseContext()
.getResources().getConfiguration();config.locale = locale;getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
呼啦一阵风
TA贡献1802条经验 获得超6个赞
@SuppressWarnings("deprecation")public static void forceLocale(Context context, String localeCode) {
String localeCodeLowerCase = localeCode.toLowerCase();
Resources resources = context.getApplicationContext().getResources();
Configuration overrideConfiguration = resources.getConfiguration();
Locale overrideLocale = new Locale(localeCodeLowerCase);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
overrideConfiguration.setLocale(overrideLocale);
} else {
overrideConfiguration.locale = overrideLocale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.getApplicationContext().createConfigurationContext(overrideConfiguration);
} else {
resources.updateConfiguration(overrideConfiguration, null);
}}
不负相思意
TA贡献1777条经验 获得超10个赞
configuration.locale
configuration.setLocale(locale);
@SuppressWarnings("deprecation")private void setLocale(Locale locale){
SharedPrefUtils.saveLocale(locale); // optional - Helper method to save the selected language to SharedPreferences in case
you might need to attach to activity context (you will need to code this)
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
configuration.setLocale(locale);
} else{
configuration.locale=locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
getApplicationContext().createConfigurationContext(configuration);
} else {
resources.updateConfiguration(configuration,displayMetrics);
}}2018年5月11日编辑
@Overrideprotected void attachBaseContext(Context base) {
super.attachBaseContext(updateBaseContextLocale(base));}private Context updateBaseContextLocale(Context context) {
String language = SharedPrefUtils.getSavedLanguage(); // Helper method to get saved language from SharedPreferences
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResourcesLocale(context, locale);
}
return updateResourcesLocaleLegacy(context, locale);}@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);}@SuppressWarnings("deprecation")
private Context updateResourcesLocaleLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;}setLocate(locale)
- 3 回答
- 0 关注
- 570 浏览
添加回答
举报
0/150
提交
取消
