Android-多次切换RTL和LTR时布局方向错误 [英] Android - Wrong layout direction when switching RTL vs LTR many times

查看:429
本文介绍了Android-多次切换RTL和LTR时布局方向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在EN和AR之间(在运行时)切换我的应用程序语言时,视图通过从LTR移到RTL来正确运行,但是当我开始通过多次切换语言来强调应用程序时,布局方向变得混乱并且得到RTL应用应该显示LTR元素时的元素.有时布局方向不再刷新.

When I switch my app language between EN and AR (during runtime) the views behave correctly by moving from the LTR to the RTL but when I start stressing the app by switching languages many times the layout direction become missy and I get RTL elements when the app is supposed to show LTR elements. Sometimes the layout direction is not refreshed anymore.

作为解决方法,我必须以编程方式为这些视图设置布局方向. binding.spinner.setLayoutDirection(LocaleUtil.isRtl(this) ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);

As workaround I have to set the layout direction programmatically for those views. binding.spinner.setLayoutDirection(LocaleUtil.isRtl(this) ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);

请问您有什么主意吗?我试图避免这种解决方法:(如果可以完全依赖Android UI呈现.

Do you have any idea please ? I am trying to avoid this kind of workaround :( If it is possible to rely entirely on the Android UI rendering.

推荐答案

我无法想象一个用户会多次切换语言,然后每次更改都会返回到应用程序:)

I can't imagine a user who will switch language many times and then returning to app on every change :)

但是,无论如何,如果Android无法正确地重新创建您的活动,则始终可以通过为您的 AndroidManifest.xml 中的"android:configChanges" 进行手动设置活动,然后在此类活动中监听 onConfigurationChanged(Configuration newConfig)方法:

But, anyway, if Android is not able to recreate your activities correctly you always can do it manually by setting "android:configChanges" in AndroidManifest.xml for your activities and then listening for onConfigurationChanged(Configuration newConfig) method in such activities:

AndroidManifest.xml 中:

<activity android:name=".MyActivity"
          android:configChanges="layoutDirection">

在为其设置了 android:configChanges 活动中:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        // change directions for you views to RTL
    } else {
        // change directions for you views to LTR
    }
}

这篇关于Android-多次切换RTL和LTR时布局方向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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