Android布局方向在运行时语言更改时未更新

7
我正在研发一款支持两种不同语言的应用程序 -
  1. 英文
  2. 阿拉伯语
用户可以随时切换到任意语言,并且根据选择我会更改应用程序的语言。
以下是代码片段 -
public static void setLocale(String languageCode, Context ctx) {

    Locale locale = new Locale(languageCode);
    Locale.setDefault(locale);

    if (Build.VERSION.SDK_INT >= VERSION_CODES.N) {
        updateResourcesLocale(ctx, locale);
    } else {
        updateResourcesLocaleLegacy(ctx, locale);
    }
}


private static void updateResourcesLocale(Context context, Locale locale) {
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    context.getApplicationContext().createConfigurationContext(configuration);
}

private static void updateResourcesLocaleLegacy(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    configuration.setLayoutDirection(locale);
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

在应用程序的每个活动中,我已经重写了attachBaseContext方法。 我在这里使用了以下逻辑 -

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(ContextWrapper.onAttach(base, LanguageUtil.getSelectedLanguageFromSharedPreferences(base)));
}
这是实用类 -
public class ContextWrapper {

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

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

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

@TargetApi(Build.VERSION_CODES.N)
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);
}

@SuppressWarnings("deprecation")
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;

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

    return context;
}

问题是 - 如果我切换到阿拉伯语,布局方向会改变为RTL,这是可以预料的。然后我再次将语言切换回英语。但是这次,布局方向没有更新。它仍然是RTL,但应该是LTR。此时,如果我将语言更改为阿拉伯语,则布局方向会变为LTR,这又是错误的。我在清单文件的应用程序标记中添加了以下属性 -
android:supportsRtl="true"

另外,我已将所有左对齐改成了 start,右对齐改为 end。

在调试过程中观察到布局方向已正确保存。但是它在用户界面中没有反映出来。

我的minSdkVersion 19 & targetSdkVersion 27

我被卡在这个问题上,无法解决。有我漏掉的东西吗?


也许这个问题可以帮到你!请阅读它的第二个回答,只有两个赞。 - Anonymous
遇到了相同的问题,但你的解决方案对我的情况没有起作用。 - Usman Rana
2个回答

19

经过许多尝试和错误,我通过在所有活动的onCreate方法中,在调用setContentView之前添加以下代码,最终解决了这个问题。

最后,通过在所有活动的onCreate方法中,在调用setContentView之前添加以下代码,我终于解决了这个问题。

     if (BuildConfig.FLAVOR_country.equals("saudi") && "ar".equals(LanguageUtil.getSelectedLanguageFromSharedPreferences(this))) {
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
     } else {
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
    }

1

将以下代码添加到您的布局顶部属性中android:layoutDirection="locale",而不是在Java文件中添加那么多代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接