Android RTL 文本方向

Android RTL Text Direction

我试图在我的应用程序中支持 RTL 语言环境,但是我 运行 遇到了以下问题。现在我说问题,但由于我不熟悉 RTL 语言环境,它甚至可能不是问题,所以就这样吧。

以下是 3 个文本视图。请注意,以下输出是在通过开发选项强制启用 RTL 的情况下生成的。

英语 LTR 测试

强制英语 RTL 测试

我发现它 st运行ge 它没有向右对齐,即使我说 android:gravity="start" 就像它如何与填充和边距等一起工作所以我查看了其他答案和遇到 android:textDirection="locale".

强制使用文本方向区域设置进行英语 RTL 测试

现在这在 LTR 和 RTL 上都正确镜像,但后来我注意到标点符号出现以下行为。这是否正确引起了人们的注意,因为我在与 Google Play Store、Facebook 和 Whats App 进行比较时强制使用 RTL,而在查看带有标点符号的段落时我没有看到这一点,这让我相信我做错了什么.

完整布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:text="@string/test_one"
        android:textColor="@android:color/black"
        android:textDirection="locale"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="start"
        android:text="@string/test_two"
        android:textColor="@android:color/black"
        android:textDirection="locale"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="start"
        android:text="@string/test_three"
        android:textColor="@android:color/black"
        android:textDirection="locale"
        android:textSize="24sp" />

</LinearLayout>

其他代码

下面是我的manifest文件里设置的

 android:supportsRtl="true"

这是我的琴弦

<string name="test_one">Good Job. You did well!</string>
<string name="test_two">3 hours ago</string>
<string name="test_three">Select Option:</string>

请原谅这听起来多么幼稚,并赐教!我希望提供舒适的 RTL 支持!

android:textDirection="locale" 添加到 TextViewandroid:supportsRtl="true" 即可。但是,textDirection="locale" 改变了方向以适应 enfaar 的区域设置。 (取决于设备的语言

明确地说,它工作正常 因为你在 TextView 中的文本是 LTR 并且你使用了 RTL direction 如您所见,它使 direction RTL 没有问题。此外,使用 android:gravity="start" 可能会强制它从头开始使用,我想,它将从 LTR 开始。 (我建议删除 android:gravity="start" 并让 Android(为了设备的语言)决定哪个是最好的)

因此,要测试它是否有效,您可以将文本设置为阿拉伯语或波斯语,如下所示:

android:text="این یک تست است"

如果它从 RTL 开始并使用 "این",那么它会按预期工作。