安卓RTL文本方向

9
我正在尝试在我的应用程序中支持RTL语言环境,但我遇到了以下问题。现在我说问题,但由于我不熟悉RTL语言环境,它可能甚至不是一个问题,所以就这样吧。
下面是3个文本视图。请注意,以下输出是通过dev选项强制开启RTL生成的。 英文LTR测试

enter image description here

测试英文RTL强制

enter image description here

我发现即使我使用了android:gravity="start",它仍然没有对齐到右侧,就像padding和margin等一样。因此,我查看了其他答案,并找到了android:textDirection="locale"使用文本方向语言强制进行英语RTL测试

enter image description here

现在这个布局在LTR和RTL上都正确地进行了镜像,但是我注意到了标点符号的以下行为。这让我怀疑是否正确,因为当我强制使用RTL查看带有标点符号的段落时,与Google Play Store、Facebook和Whats App进行比较时,我没有看到这种情况,这让我相信我做错了什么。
完整的布局代码
<?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>

其他代码

以下内容设置在我的清单文件中

 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支持!

1个回答

18

android:textDirection="locale"添加到TextViewandroid:supportsRtl="true"将解决问题。但是,textDirection="locale"会根据语言环境更改方向,其中包括enfaar。(取决于设备的语言

要清楚明确,这样运行正常,因为你在TextView中的文本是LTR,并且你使用了RTL方向,正如你所看到的,它使方向成为RTL,没有任何问题。此外,使用android:gravity="start"可能会强制从开始使用,我想它将从LTR开始。(我建议删除android:gravity="start",让Android(根据设备的语言)决定哪个是最好的)

因此,要测试是否有效,请将文本设置为阿拉伯语或波斯语,如下:

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

如果它从RTL开始,并以"این"结尾,则它会按预期工作。


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