Android 配置更改导致布局方向更改

Android Configuration Changes lead to change in layout direction

我在某些设备(如 Note3 的纵向模式设计)上遇到问题,只有当我将应用程序的语言更改为 RTL 时,它才支持 RTL 和 LTR,但当我用相机拍照时,设备旋转,当返回到应用程序时,应用程序忽略定义的区域设置并使用英语的设备区域设置,应用程序的方向更改为 LTR,但使用 RTL 的字符串,直到您导航到另一个屏幕,所有应用程序都将变为 LTR只有当设备语言环境仅为英语时才会发生这种情况。 当设备语言环境是阿拉伯语时,这不会发生。 这是我的语言环境代码

public class MvpApp extends Application {
AppDataManager dataManager;
private Locale locale = null;
private String language;

@Override
public void onCreate() {
    super.onCreate();

    Fresco.initialize(this);
    final Fabric fabric = new Fabric.Builder(this)
            .kits(new Crashlytics())
            .debuggable(true)
            .build();
    Fabric.with(fabric);
    AppPreferencesHelper preferencesHelper = new AppPreferencesHelper(getApplicationContext());
    ApiModule apiModule = new ApiModule(getApplicationContext());
    dataManager = new AppDataManager(getApplicationContext(),
            preferencesHelper, apiModule.provideApiService());

    String lang = dataManager.getLanguage();

    if (lang == null)
        language = "en";
    else language = lang;

    LocaleHelper.setLocale(getApplicationContext(),language);


}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (locale != null) {
        newConfig.locale = locale;
        Locale.setDefault(locale);
        newConfig.setLayoutDirection(locale);
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
    }
}


public AppDataManager getDataManager() {
    return dataManager;
}

和语言环境助手class

public class LocaleHelper {

public static Context onAttach(Context context, String language) {
    return setLocale(context, language);
}

public static Context setLocale(Context context, String localeSpec) {
    Locale locale = new Locale(localeSpec);
    Locale.setDefault(locale);

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

}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    configuration.setLayoutDirection(locale);

    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, Locale locale) {
    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    configuration.setLayoutDirection(locale);
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return context;
}

}

我已经设法解决了这个问题,但在活动中覆盖了 onConfigurationChanged 并在清单中添加了这一行

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Locale locale = new Locale(presenter.getLanguage());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        newConfig.setLocale(locale);
        Locale.setDefault(locale);
        newConfig.setLayoutDirection(locale);
        this.createConfigurationContext(newConfig);
    } else {
        newConfig.locale = locale;
        Locale.setDefault(locale);
        newConfig.setLayoutDirection(locale);
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
    }
}