How to override RTL support on a layout in Android

♀尐吖头ヾ 提交于 2019-12-07 03:54:10

问题


I set android:supportsRtl="true" in the <application> tag in AndroidManifest.xml, but I need to force one of the views to be left-to-right nonetheless, even when the language of the interface is Hebrew or Arabic. How can I force a specific view to be LTR in an RTL application?

Specifically, I want to force some linear layout to go left-to-right instead of the default right-to-left even when the language is right-to-left.


回答1:


Generally gravity="left" is enough to force text to be left-to-right. But it didn't help with the direction of a linear layout. The solution was to add android:layoutDirection="ltr".




回答2:


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="ltr">

for letf to right all layout content.




回答3:


To complete the answers, aside from XML, layout direction can also be changed programmatically with ViewCompat.setLayoutDirection(view, LayoutDirection.RTL). This API can be used from API 19 and onwards, so If your min sdk version supports API below 19, an if-check needs to be performed:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewCompat.setLayoutDirection(...)



回答4:


If you want to force your application to ltr even on rtl languages you don't need layout direction (it works on api>17) you can just set android:supportsRtl to false.



来源:https://stackoverflow.com/questions/29466048/how-to-override-rtl-support-on-a-layout-in-android

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