【问题标题】:how to force language in android application [duplicate]如何在android应用程序中强制语言[重复]
【发布时间】:2011-09-19 06:47:39
【问题描述】:

可能重复:
Changing Locale within the app itself

在我的应用程序中,我需要“强制”语言,假设我有英语语言环境作为默认波兰语和芬兰语,根据that post 我创建的函数也在下面发布,函数在 createActivity() 中调用,但是问题是它不起作用....知道为什么吗?有什么建议吗?

private void setLocale(String localeCode){
        Locale locale = new Locale(localeCode);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

【问题讨论】:

    标签: android locale


    【解决方案1】:

    这是我到目前为止所得到的。我知道这个问题已经解决,但我的解决方案更简单、更紧凑。不需要其他更改,清单中的所有活动都不需要 android:configurationChanges 属性。

    public class SigmaMiddleEastApplication extends PPGApplication {
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            newConfig.locale = Locale.ENGLISH;
            super.onConfigurationChanged(newConfig);
    
            Locale.setDefault(newConfig.locale);
            getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
        }
    }
    

    注意这可能会导致问题: What could cause an Android activity to relaunch itself infinitely when returning from camera?

    【讨论】:

    • 只需将所有内容都包裹在“if(newConfig.locale!=targetLocale)”中即可解决无限重启问题。
    【解决方案2】:

    在清单中添加以下内容(针对每个活动):

    android:configChanges="locale"
    

    【讨论】:

    • 为此必须与以下解决方案结合使用(由 Sergii 提供)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2021-04-13
    • 2016-05-01
    • 2018-08-31
    • 2012-09-16
    • 2012-08-11
    相关资源
    最近更新 更多