更改语言环境:强制活动重新加载资源? [英] Changing locale: Force activity to reload resources?

查看:14
本文介绍了更改语言环境:强制活动重新加载资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序中有一个语言设置.切换语言时,我希望所有文本视图等立即更改语言.目前我只是在配置中更改了语言环境,所以当用户重新启动活动时,语言已经改变了.

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.

我的问题的一个丑陋的解决方案是让每个文本视图在每次更改语言时都加载新资源.有更好的解决方案吗?也许是一种巧妙的方式来离散地重新启动活动?或者只是强制重新加载资源?

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?

推荐答案

在您的 AndroidManifest.xml 中,将此属性添加到您的 Activity

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);
}

https://developer.android.com/guide/topics/manifest/activity-element.html#config

这篇关于更改语言环境:强制活动重新加载资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆