0

我正在使用以下方法以编程方式更改我的应用程序的语言环境。它工作正常,但是当我使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 标志启动已经存在的singleTask活动时。然后应用程序失去了布局方向,但翻译是正确的。例如,如果应用程序语言是阿拉伯语,则所有视图方向都更改为英语区域设置(从左到右)。可能是什么原因?

我在 BaseActivity 和 Application 类的 attachBaseContext 中调用以下方法。

public Context createLocaleConfiguration(Context context,String language) {
   
        Locale newLocale = new Locale(language);
        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(newLocale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
            configuration.setLayoutDirection(newLocale);
            context = context.createConfigurationContext(configuration);

        } else {
            configuration.setLocale(newLocale);
            configuration.setLayoutDirection(configuration.locale);
            Locale.setDefault(newLocale);
            context = context.createConfigurationContext(configuration);
        }
        return context;
    }


@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(LocaleManager.getInstance().createLocaleConfiguration(newBase,language));
}

任何想法都将是可观的。谢谢

4

1 回答 1

0

我想你应该尝试使用一些像这样的帮助类:

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static Context onAttach(Context context) {
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        return setLocale(context, lang);
    }

    public static Context onAttach(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        return setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static Context setLocale(Context context, String language) {
        persist(context, language);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        }

        return updateResourcesLegacy(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);

        return context.createConfigurationContext(configuration);
    }

    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }
}

而不仅仅是在你的应用程序和基础活动类中使用它:

  override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LocaleHelper.onAttach(newBase))
    }
于 2020-07-08T06:05:21.080 回答