Android - Wrong layout direction when switching RT

2019-07-20 17:55发布

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.

2条回答
看我几分像从前
2楼-- · 2019-07-20 18:25

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>
查看更多
不美不萌又怎样
3楼-- · 2019-07-20 18:35

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
    }
}
查看更多
登录 后发表回答