Android:约束布局支持RTL吗?

22
在Android Studio中支持RTL的最佳实践是什么?还是应该创建一个专门用于英语和阿拉伯语的分离布局?

English version

阿拉伯语言的预期布局 阿拉伯语言的预期布局

将设备语言从英语更改为阿拉伯语时的输出布局 将设备语言从英语更改为阿拉伯语时的输出布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="@string/CourseName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <Button
        android:text="@string/enroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
</android.support.constraint.ConstraintLayout>

8
"зәҰжқҹеёғеұҖж”ҜжҢҒRTLеҗ—пјҹ"-- жҳҜзҡ„пјҢеҰӮжһңдҪҝз”Ё...Startе’Ң...End规еҲҷиҖҢдёҚжҳҜ...Leftе’Ң...Right规еҲҷгҖӮ - CommonsWare
实际上,RTL 用于支持不同的语言,例如英语、阿拉伯语等。因此,在 LTR 视图中,您的文本应该是英语而不是阿拉伯语,就像您所做的那样。 - Atef Hares
我需要输出布局与预期布局相同。第一张图片是预期布局,但第二张图片是我得到的布局。 - manar musTapha
@MartinMarconcini:ConstraintLayout具有layout_constraintStart_toEndOflayout_constraintStart_toStartOflayout_constraintEnd_toStartOflayout_constraintEnd_toEndOf属性,与我的评论一致。 - CommonsWare
我不知道ConstraintLayout在RTL-LTR方面的作用是什么(我一直想知道为什么事物不是以start/end而不是left/right为基础),但在你的情况下,我认为一个小的RelativeLayout会更好地为你服务。 - Martin Marconcini
显示剩余3条评论
4个回答

19

正如CommonsWare指出的那样,如果您计划支持RTL语言,特别是目标API 16+,您应该很少或从不使用Left/Right。

app:layout_constraintEnd_toEndOf="parent"等替换app:layout_constraintRight_toRightOf="parent"

对于Right使用End,对于Left使用Start。


嘿,怎么按照指南让它正常工作?当我更改语言时,它不会转移到屏幕的另一侧,这会破坏布局。 - zarcel
@zarcel 我不知道。我在我的布局中没有使用指南。抱歉! - Martin Marconcini
1
@zarcel 我不得不使用指南和RTL/LTR支持,我所做的是将指南位置存储在values/dimens.xml中,然后在values-ldrtl/dimens.xml中覆盖它们。因此,对于LTR,假设您在左侧距离25%处有一个指南,另一个在75%处,您可以定义:<item type="fraction" name=“guideline_one”>0.25</item><item type="fraction" name=“guideline_two">0.75</item>。在values-ldrtl文件夹中,您需要复制它们并放置相反的值:guideline_one变成100-25=75 -> 0.75,而guideline_two变成100-75=25,因此为0.25 - Martin Marconcini

5
很不幸,他们没有做对(或者说,“结束”;-))。在许多约束条件中,例如针对父级的约束条件,Android Studio使用“左”/“右”,而应该分别使用“开始”/“结束”。这会导致RTL语言(其字母表为中东)出现问题。更糟糕的是:我甚至没有看到任何关于此问题的公开错误报告。我的解决方案是:关闭Android Studio(这样它就不会在我编辑时跳来跳去尝试修复问题),然后使用Atom和RegExp(或sed命令)小心地将“layout_constraintLeft_toLeftOf”替换为“layout_constraintStart_toStartOf”,并将“layout_constraintRight_toRightOf”替换为“layout_constraintEnd_toEndOf”。抱歉,没有找到更简单的方法 :-(
请注意:使用链接时情况更糟。RTL布局甚至无法很好地进行链接,也无法维护布局。我的结论是,在Google提供成熟的RTL语言支持之前,回到旧的布局...毕竟,RTL比LTR存在得更久 ;-)
更新:解决方案已经接近了。我向Google提交了一个错误报告。他们说这将在“2.4中修复”。
更新:标记为已解决,于2017年4月25日修复。

1
还有其他表达式“layout_constraintRight_toLeftOf”和“layout_constraintLeft_toRightOf”,在RTL中也起到作用,请在您的sed命令中包括这些内容 :) - Amit Kaushik
不要让 Google 开发人员抓到你说这话!大多数情况下不要那些在 AndroidStudio 检查中放置警告的人 "...硬编码...某事" ;-) 是的,你可以为 RTL 编写一个新布局... 但是...何必在救援即将到来时继续维护 2 个布局文件呢? - Meymann
@Meymann,您能否提供此问题的链接?(我的意思是_他们说它将在“2.4中修复”._) - Dr.jacky
1
注意:您可以在Android Studio中找到/替换(只需关闭预览)。链式布局:您可以在RTL中正确地链接,解决方案(迄今为止)(1.0.3 beta 3)包括将链式定义放在链式的“头”和最右边的项目(水平)或最底部(垂直)。因此,您实际上正在两个元素中复制链式样式。目前,“有问题”的一个被ConstraintLayout忽略了(我观察不到任何副作用),并且在RTL中,链式保持不变。 - Martin Marconcini
@Mr.Hyde - 这里有一份通讯记录,位于 https://issuetracker.google.com/issues/37324626 但我退缩了。没有时间等待他们,所以我仍然使用旧的布局系统……稳如泰山。 - Meymann
@Meymann 很好的信息,我在这里报告了CL + RTL问题 https://issuetracker.google.com/issues/70181930 和 https://issuetracker.google.com/issues/70172298 和 https://issuetracker.google.com/issues/70243928,所有问题都将在下一个beta 4中修复(或者它说是这样的,但我无法在当前的专业版中使用Beta,所以我仍然需要解决这些问题)。 - Martin Marconcini

1

在此输入图像描述设计模式尚不支持RTL指南。

简单解决方案- 清理项目,以便不再存在生成的文件。 在您的xml文件中将所有“Left”替换为“Start”,将所有“Right”替换为“End”。 注意-大小写敏感

就是这样!!


希望在2.4版本中能做到这一点,那将非常好。但目前来看,在RTL上即使是等距离间隔也会破坏你的布局……你无法很好地预览它。这让我感到非常可怕,以至于我选择放弃使用约束视图,转而使用旧的布局元素。 - Meymann

0

是的,约束布局支持RTL,但使用RTL时应该使用“开始和结束”约束代替“左和右”约束。

例如: 使用 app:layout_constraintEnd_toEndOf="parent" 而不是 app:layout_constraintRight_toRightOf="parent"

                  AND

                  Use

使用app:layout_constraintStart_toStartOf="parent"代替app:layout_constraintLeft_toLeftOf="parent"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接