更改应用程序区域设置有时不会更改布局方向 [英] Changing app locale sometimes doesn't change direction of layouts

查看:122
本文介绍了更改应用程序区域设置有时不会更改布局方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm changing locale of my app programmatically using the following method. It works fine but when I start an already existing singleTask activity using Intent.FLAG_ACTIVITY_CLEAR_TOP flag. Then application loses the layout direction but translation is correct. For example, if application language is Arabic then all views direction is changed to English locale (left-to-right). What could be the reason?

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

I'm calling following method in attachBaseContext of BaseActivity and Application class.

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));
}

任何想法都是可取的. 谢谢

Any ideas would be appreciable. Thanks

推荐答案

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

I guess you should try to use some helper class like this one:

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;
    }
}

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

And than just use it in you application and base activity classes like that:

  override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LocaleHelper.onAttach(newBase))
    }

这篇关于更改应用程序区域设置有时不会更改布局方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆