改变区域:强制行为重新加载的资源?加载、区域、行为、资源

2023-09-12 09:16:23 作者:烟’刺痛了眼

所以,我在我的应用程序中的语言设置。当切换语言,我想所有的textviews等立即改变语言。目前,我只是改变了语言环境中的配置,因此当用户重新启动该活动的语言发生了变化。

So I have a language setting in my application. When the language is switched, I would like all the textviews etc to change language immediately. Currently I just change the locale in the configuration, so the language has changed when the user restarts the activity.

这是丑陋的解决我的问题将是使每一个TextView的语言改变时加载新的资源。有没有更好的解决办法?也许是一种巧妙的方法离散重启活动?或者,也许只是迫使资源重装?

An ugly solution to my problem would be to make each textview load the new resources each time the language is changed. Is there a better solution? Perhaps a neat way to discretely restart the activity? Or maybe just force reload of the resources?

干杯,

推荐答案

在你的Andr​​oidManifest.xml中,这个属性添加到您的活动

In your AndroidManifest.xml, add this attribute to your Activity

android:configChanges="locale"

在你的活动覆盖 onConfigurationChanged()

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
  super.onConfigurationChanged(newConfig);
}

http://developer.android.com/intl/de/guide/topics/manifest/activity-element.html#config