如何从SettingsActivity更改应用程序的区域设置?

时间:2015-09-25 20:17:23

标签: android

我想从设置中更改应用程序的语言。

    public boolean onPreferenceChange(Preference preference, Object value) {
                String stringValue = value.toString();

                if (preference instanceof ListPreference) {
                    // For list preferences, look up the correct display value in
                    // the preference's 'entries' list.
                    ListPreference listPreference = (ListPreference) preference;
                    int index = listPreference.findIndexOfValue(stringValue);

                    // Set the summary to reflect the new value.
                    preference.setSummary(index >= 0 ? listPreference.getEntries()[index]: null);

                    .............

此处,在设置活动中,我们如何访问应用程序的配置和区域设置以更改其语言。

仅供参考,我已在字符串中翻译了字符串。

1 个答案:

答案 0 :(得分:0)

检查此代码

  public static final String ARABIC = "ar";
    public static final String ENGLISH = "en";
 public static void changeLoc(Activity context, int flagLang)
    {
        Resources res = context.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();

        if(flagLang == 0)
        {
            //english
            conf.locale = new Locale(ENGLISH);

        }
        else
        {
            //arabic
            conf.locale = new Locale(ARABIC);
        }
        res.updateConfiguration(conf, dm);
    }