android右到左布局未能切换,Android-多次切换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 l
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.
解决方案
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:
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
}
}
更多推荐
所有评论(0)