我尝试在android应用程序中更改我的区域设置。如果我只使用语言,一切都可以,但今天我在我的应用程序中添加了葡萄牙语-br翻译。
代码:
Locale locale;
if(language.contains("-")) // In this case, the locale specify also the country
{
String[] country_locale = language.split("-");
locale = new Locale(country_locale[0], country_locale[1]);
}
else
locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());一切都好,直到最后一行。我之所以知道,是因为在我的前一段程序中,我可以使用以下代码获得一些具有正确pt-br区域设置的字符串:
Resources resources = new Resources(ctx.getAssets(), ctx.getResources().getDisplayMetrics(), new_config);如果区域设置是使用国家/地区代码定义的,则updateConfiguration会将区域设置设置为英语。
String.xml的值为-pt-rBR
而调试区域设置值设置为pt-BR。
编辑:经过进一步测试,它可以在我的Android手机上运行,但不能在我的平板电脑上运行( Sambung和android 4.4.2)。可能的原因是什么?
编辑2:如果我使用手机,也可以通过模拟器工作,如果我使用平板电脑,则不能工作。
发布于 2016-02-23 20:56:28
在您当前的活动中,尝试在代码后面调用以下代码:
finish();
startActivity(getIntent());这将重新创建您的活动,然后重新加载字符串资源。
https://stackoverflow.com/questions/28886896
复制相似问题