android - RTL 对齐不适用于长文本

标签 android right-to-left text-alignment

我正在设计一种布局,该布局在不同的 View 中同时包含英语和希伯来语(旨在从右到左)文本。当一行希伯来语文本超过一定长度时,它会变成从左到右书写(我认为这与长度有关,因为它在较短的文本上看起来很好,而且当我在平板电脑而不是手机上显示它时). 相关 View 如下所示:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:gravity="center"
    android:textDirection="rtl"
    android:text="@string/tx_middle_text"
    android:textSize="@dimen/big_text_dp" />

字符串在 strings.xml 中定义如下:

<string name="tx_middle_text">טקסט בעברית</string>

(我已经替换了原来的文本,它有 50 个字符长,完全由希伯来字母和空格组成)。
请注意文本具有 rtl 属性。我试过用 anyRtl 替换它,我也试过将重力改为“正确” - 都没有用。
我需要文本保持在一行中,如果不合适则用省略号将其截断 - 事实上,这就是发生的情况,但文本是从左到右书写的。
我该如何解决这个问题?

编辑:对于一个临时解决方案,我制作了一个较短的字符串作为较小布局的替代资源(只要文本在给定设备上的长度小于一行,它就可以工作),但我仍然喜欢了解是否有通用的解决方案。

最佳答案

RTL 由系统根据字符自动检测。即对于阿拉伯字符,文本将从右到左绘制。

这是一个关于如何绘制微调器的示例。对于这个 TextView:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_item"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="15sp"
    android:gravity="center_vertical|start"
    android:textColor="@color/spinner_filter_text"
    android:ellipsize="end"
    android:singleLine="true"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"/>

还有这些字符串:

<string name="all_categories">All Categories</string> (值)

enter image description here (值-ar)

结果是:

enter image description here enter image description here

如果我修改 TextView 为:

android:layout_width="100dp"

结果是:

enter image description here enter image description here

我认为这很好...请注意,在 strings.xml 中,ar 语言的字符串方向是错误的,但 Android 正在正确绘制它。我猜这是因为一些 Android Studio 设置。

关于android - RTL 对齐不适用于长文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513588/

相关文章:

android - 时代广场日历XML文件错误

css - 创建一个 css 显示 :block that is right-to-left

ios - 适用于 RTL 语言的可靠 iOS 图像翻转

r - 如何在ggplot2注释中左对齐文本

css - 将图像与一列文本对齐,同时保持响应

android - Proguard 混淆不起作用

java - 如何使用 ClickListener 在 Android Studio 中设置起始标记位置?

html - 为什么 Flexbox 中的强文本表现为列?

android - Branch.io 延迟深度链接在 Facebook 广告中不起作用

php - RTLIT css padding rtl 正则表达式