重新启动应用程序即可实时反映UI语言更改

重新启动应用程序即可实时反映UI语言更改

本文介绍了android:无需重新加载/重新启动应用程序即可实时反映UI语言更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个设置,允许用户选择不同的本地化(语言),即Chinese, German, etc.

I have a setting in my app that allows user to select different localization (language), ie Chinese, German, etc.

我想做的是,一旦用户做出选择,立即使用当前所选语言的字符串更新布局.当然,我希望在不重新加载应用程序的情况下将lang变化传播到所有当前活动.

What i would like to do is that once the user makes their choice, to immediately update the layout with strings in the currently selected language. Of course, i want the lang change propagated to ALL current activities, without reloading the app.

我发现了这一点(已经尝试过了),但是想知道是否有更清洁的方法.

I found this (havent tried yet), but was wondering if there is a cleaner way of doing it.

http://www.tutorialforandroid.com/2009/01/force-localize-application-on-android.html

Gracias

推荐答案

我也遇到了这个问题.我使用下面的代码,然后在不刷新活动的情况下更改了语言

I also had this issue.I used the code below and then it changed the language without refreshing the activity

public void setLocale(String lang) {

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    onConfigurationChanged(conf);
    /*Intent refresh = new Intent(this, AndroidLocalize.class);
    startActivity(refresh);*/
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
    lblLang.setText(R.string.langselection);
  super.onConfigurationChanged(newConfig);
}

我希望它能对您有所帮助....

I hope it would help you.......

这篇关于android:无需重新加载/重新启动应用程序即可实时反映UI语言更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:31