Android - Wrong layout direction when switching RTL vs LTR many times

北城余情 提交于 2019-12-11 02:08:34

问题


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.

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

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.


回答1:


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

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:

In AndroidManifest.xml:

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

In Activity for which you set 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
    }
}



回答2:


make sure that supportsRtl="true"

<application
    android:supportsRtl="true"
    android:name=".App"
    android:icon="@drawable/news"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
</application>


来源:https://stackoverflow.com/questions/41644936/android-wrong-layout-direction-when-switching-rtl-vs-ltr-many-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!