屏幕旋转后,我的应用程序的语言将被更改 [英] After the screen rotation, the language of my application will be changed
问题描述
我创建了一个双语(具有两种语言)的android应用程序.我将资源字符串插入了两个文件:
I have created a bilingual (having two languages) android application. I have inserted my resource strings in two files:
For Persian language (default)
values/strings_locale.xml
For English language
values-en/strings_locale.xml
在我的第一个启动活动中,我插入了以下代码:
I my first launching Activity I have inserted the following code:
Configuration c = new Configuration(this.getResources().getConfiguration());
c.locale = new Locale("fa_IR");
this.getResources().updateConfiguration(c, this.getResources().getDisplayMetrics());
因此,在此代码之后,我的默认语言将为波斯语,并且所有活动"中的所有字符串均以波斯语正确显示.
So after this code, my default language will be Persian and all of strings in all of Activities are shown in Persian language correctly.
但是问题是,当旋转设备屏幕时,所有字符串都以英语显示,并且所有其他活动也都发生这种情况!而且我必须关闭并重新打开我的应用程序.
But the problem is when the screen of the device is rotated, all of the strings are shown in English and it also happens for all other Activities! And I have to close and reopen my application.
为什么会发生这种情况以及如何解决此问题?
Why this happens and how I can solve this problem?
推荐答案
您可以创建扩展Application
的类.在那里,您可以覆盖每次更改配置时都会调用的一种方法(例如,当屏幕方向更改时).
You can make class which extends Application
. There you can override one method which gets called everytime you change configuration (example when screen orientation gets changed).
类似的东西:
public class MyApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLocale();
}
private void setLocale() {
Locale locale = new Locale("fa_IR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
也不要忘记在清单中声明它: Application类的示例
And dont forget to declare it in the manifest: example of Application class
在AndroidManifest.xml中:
In AndroidManifest.xml:
<application
android:name="com.blaablaa.bumbam.MyApplication"
...
这篇关于屏幕旋转后,我的应用程序的语言将被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!