android Oreo中的RTL布局错误

Usm*_*uri 6 android android-layout android-8.0-oreo

自从我在移动设备上升级到android oreo后,我的RTL支持应用程序无效.它正在将字符串更改为阿拉伯语,但不会更改布局方向.但如果我将同一个RTL转移到低于oreo的任何设备,一切正常.其他人遇到过这个问题?关于这个bug和解决方法还有任何官方声明吗?

下面是我更改区域设置的方法

public static boolean setDefaultLocale(Context context) {
    Resources resources = context.getResources();
    PreferenceManager preferenceManager = PreferenceManager.getInstance();
    String localLanguage = resources.getConfiguration().locale.getLanguage();
    boolean isLanguageChanged = !preferenceManager.getCurrentLanguageCode().equalsIgnoreCase(localLanguage);
    if (isLanguageChanged) {
        Log.d("", preferenceManager.getCurrentLanguageCode());
        Locale locale = new Locale(preferenceManager.getCurrentLanguageCode());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            Locale.setDefault(Locale.Category.DISPLAY, locale);
        else
            Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        ((Activity) context).recreate();
    }
    return isLanguageChanged;
}
Run Code Online (Sandbox Code Playgroud)

amo*_*new 11

在onCreate函数中进行简单修复即可添加以下代码:

if (Locale.getDefault().getLanguage()=="ar")
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
else
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的主意!我们最终得到了更通用的版本:`getWindow().getDecorView().setLayoutDirection(getResources().getConfiguration().getLayoutDirection());` (2认同)

Usm*_*uri 5

感谢@amorenew并调整了Util类中的方法以支持下面的oreo中的这个奇怪的更新是你只需要在用户更改应用程序语言首选项时调用此方法onResume的工作方法

/**
 * this to change app language to the saved language in user preferences
 *
 * @param context
 * @return
 */
public static boolean setDefaultLocale(Context context, boolean isClearData) {
    Resources resources = context.getResources();
    Resources resourcesApp = context.getApplicationContext().getResources();
    String localLanguage = resources.getConfiguration().locale.getLanguage();
    boolean isLanguageChanged = !PreferenceManager.getInstance().getCurrentLanguageCode().equalsIgnoreCase(localLanguage);
    if (isLanguageChanged) {
        Log.d("", PreferenceManager.getInstance().getCurrentLanguageCode());
        Locale locale = new Locale(PreferenceManager.getInstance().getCurrentLanguageCode());
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        resourcesApp.updateConfiguration(config, resources.getDisplayMetrics());
        //for API 25
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        context.getApplicationContext().createConfigurationContext(configuration);
        context.createConfigurationContext(configuration);

        ((Activity) context).recreate();
        if (isClearData) {
            CurrencyViewModel.getInstance().removeModel();
            CarNationalityViewModel.getInstance().removeModel();
            DialCodeViewModel.getInstance().removeModel();
        }
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            ((Activity)context).getWindow().getDecorView().setLayoutDirection(Locale.getDefault().getLanguage().equalsIgnoreCase("ar")
                    ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
        }
    }
    return isLanguageChanged;
}
Run Code Online (Sandbox Code Playgroud)