语言改变在android中不起作用?

时间:2017-03-30 17:16:52

标签: android

  • 此代码无效,有什么问题,请任何人帮帮我..

    private void setLocaleLanguage(String languageSelection) {
    myLocaleLanguage = new Locale(languageSelection);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Locale.setDefault(myLocaleLanguage);
        conf.setLocale(myLocaleLanguage);
        this.createConfigurationContext(conf);
    }else{
        conf.locale = myLocaleLanguage;
        Locale.setDefault(myLocaleLanguage);
        res.updateConfiguration(conf, this.getResources().getDisplayMetrics());
    }
    
    Intent refresh = new Intent(this, MainActivityBeforeLogin.class);
    startActivity(refresh);
    finish();
    

    }

1 个答案:

答案 0 :(得分:0)

而不是:

this.createConfigurationContext(conf);

使用:

res.updateConfiguration(conf, dm);

代码:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Locale.setDefault(myLocaleLanguage);
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}

将您的意图改为

 Intent i = new Intent(getApplicationContext(), MainActivityBeforeLogin.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);