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

时间:2018-10-01 10:38:39

标签: java android locale onconfigurationchanged

某些设备(例如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;
}

和语言环境帮助程序类

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

}

1 个答案:

答案 0 :(得分:0)

我设法克服了这个问题,但是在活动中覆盖了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());
    }
}