Android 当方向更改为横向时,语言环境会发生变化

Android locale gets changed when the orientation is changed to landscape

我正在创建一个 android 应用程序,我在其中设置了英语和泰米尔语的语言环境,当我单击泰米尔语时,它会以泰米尔语显示字符串并且一切正常 fine.I 我有一个 activity 当 activity 语言环境从泰米尔语更改为英语开始时处于横向模式,即使我已经使用

   android:configChanges="locale|orientation|screenSize|keyboardHidden" 

在清单中,但没有用。

谁能告诉我如何克服这个问题issue.I 已将语言存储在共享首选项中。

 private static MyApplication mInstance;
public static final String PREF_NAME = "CoconutMetaData";
public static final String PREF_USER_LANGUAGE_KEY = "userLanguage";
@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
    SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    String myLanguage = preferences.getString(PREF_USER_LANGUAGE_KEY,"en");

    // Set user specific locale (Language)
    Locale mLocale = new Locale(myLanguage);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLocale(mLocale);
    }
    res.updateConfiguration(conf, dm);

    /**
     * To display overflow menu when hard menu key is pressed (for devices that have hard menu key)
     */
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {

    }
}

public static synchronized MyApplication getInstance() {
    return mInstance;
}

public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
    ConnectivityReceiver.connectivityReceiverListener = listener;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

全局声明 LocalConfiguration 对象

    Locale mLocale;
    Configuration conf;

并从

设置本地
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    try {
         conf.setLocale(mLocale);
    } catch (Exception e) {
        // TODO: handle exception
    }
}

试试下面的代码,这是设置本地语言的好方法

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);
    Intent refresh = new Intent(this, AndroidLocalize.class);
    startActivity(refresh);
}

Example