强制“区域设置"在申请中 [英] Force "locale" in application
问题描述
我想旋转手机并保留语言环境.我有文件夹值,值- en 和值- hr .我参加的每项活动
I want to rotate my phone and keep the locale. I have folders values, values-en and values-hr. In every activity I have
android:configChanges="keyboardHidden|orientation|locale"
以及我拥有的每个 activity.java 文件
private Locale locale = null;
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
和onCreate
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString("language","en");
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
在我旋转手机之前,它一直有效-应用程序仍在工作,按钮和一切都很好-但是语言环境变得怪异-有时是我的语言环境(hr)有时是(en)...
It works till I rotate my phone - app is still working, buttons and everything is fine - but locale is getting weird - sometimes it's my locale (hr) sometimes it's (en)...
我还需要添加什么?
推荐答案
我只是删除了"if's",现在一切正常.是的,每次重设"语言环境都有点笨拙",但我找不到其他方法.
I simply removed "if's" and everything is working now. Yes, it is a little "bulky" every time to "re-force" a locale but I couldn't find another way.
我的解决方案略有不同,我只是在onCreate和onConfigChange上强制"它-并非每隔100毫秒左右,而在Desire(2.2)上它就可以正常工作.
My solution is a little bit different, I just "re-force" it on onCreate and onConfigChange - not every 100ms or so and on Desire (2.2) it's working flawlessly.
@Austyn
是的,这几乎是一个相同的问题,但是除了"100ms强制"之外,我还有另一种解决方案.
Yes, it's pretty much the same question but I though is there another solution except "100ms forcing".
@Mayra
用户想要使用自己的母语(HR),但(当前)手机中不支持它 ,所以这是唯一的方法-强制使用它=/
Users wanna have their mother-tongue (HR) but it's not supported in (current) mobile phones so this is the only way - to force it =/
这篇关于强制“区域设置"在申请中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!