简体   繁体   English

Android-动态更改语言

[英]Android - Dynamically change language

In my Android app i want to change the default language dinamically. 在我的Android应用中,我想动态更改默认语言。 I have implemented this method: 我已经实现了这种方法:

public void changeLanguage(String lang) {  //lang="it" or "en" for example
    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
            {
                finish();
                startActivity(getIntent());
            } else recreate();
        }
    }, 1);
}

and in the manifest i added to my MainActivity this line: 在清单中,我在MainActivity中添加了以下这一行:

android:configChanges="locale|orientation"

i also tried this: 我也试过这个:

android:configChanges="locale|layoutDirection"

This solution works well, but as soon as the screen is rotated comes back to the default configuration and the language is restored. 此解决方案效果很好,但是只要旋转屏幕,它就会恢复为默认配置并恢复语言。

How can i resolve this issue? 我该如何解决这个问题?

You could save your language configuration in the callback onSaveInstanceState , when the system re-create your activity because of rotation, reload the saved locale values. 您可以将语言配置保存在回调onSaveInstanceState ,当系统由于旋转而重新创建活动时,请重新加载保存的语言环境值。

private static final String LANG = "lang";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null) {
        Configuration configuration = getResources().getConfiguration();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        configuration.locale = new Locale(savedInstanceState.getString(LANG));
        getResources().updateConfiguration(configuration, displayMetrics);
    }
}

/*
 * (non-Javadoc)
 * 
 * @see
 * android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os
 * .Bundle)
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);

    outState.putString(LANG, "it");
}

Have you tried storing your locale in preferences? 您是否尝试过在偏好设置中存储区域设置? For example: 例如:

protected void onCreate(Bundle savedInstanceState) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    setLocale(prefs.getString("pref_locale", "en"));
}

public void changeLanguage(String lang) {
    prefs.setString("pref_locale", lang);
    setLocale(lang);
}

public void setLocale(String language_code) {
    Resources res = getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(language_code.toLowerCase());
    res.updateConfiguration(conf, dm);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM