如何强制包含 ltr 字符的 textView 在 rtl 环境中右对齐

Ata*_*ara 5 android android-layout right-to-left

所有“线”都使用相同的结构(宽度、高度、重力……:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="30dp"
  android:orientation="horizontal">

  <TextView
     android:id="@+id/labelMkt1"
     android:layout_width="0dp"
     android:layout_height="30dp"
     android:layout_weight="0.5"
     android:gravity="center_vertical"   // I also tried "center_vertical|start"
     android:text="@string/report_mkt"                       
     android:textColor="#000" /> <!-- force gravity start, textAlignment=textStart, did not work, useful for RTL layout with EN characters -->

  <TextView
    android:id="@+id/labelMkt2"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:text="@string/device_param_empty" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但是当 label1 包含英文字符(例如“MKT”)时,文本左对齐。

mkt 左对齐

  • 我尝试使用android:gravity="center_vertical|start"(尽管“开始”是默认设置)但它没有帮助。

  • 我尝试添加android:textAlignment="textStart",但没有帮助。

如何强制 label1 像所有其他标签一样右对齐?

Tam*_*bul 5

android:textDirection="rtl"无论文本是希伯来语还是英语,您都可以使用它,它会从您视图的右侧开始,并且在您更改语言时不会跳到另一个方向。

注意 -我认为这android:gravity对您不起作用,因为希伯来语是写 rtl 而英语是写 ltr。
因此,当您放置重力 - 开始并且语言为英语时,您的文本将从视图的左侧开始。

使用工作示例进行编辑:

布局:

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"
tools:context=".MainActivity">


<TextView
    android:id="@+id/labelMkt1"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="0.5"
    android:gravity="center_vertical"
    android:text="@string/report_mkt"
    android:textColor="#000"
    android:background="@color/colorPrimaryDark"
    android:textDirection="rtl" />

<TextView
    android:id="@+id/labelMkt2"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="1"
    android:background="@color/colorAccent"
    android:gravity="center_vertical"
    android:text="@string/device_param_empty"
    android:textDirection="rtl" />
Run Code Online (Sandbox Code Playgroud)

字符串资源:

<resources>
<string name="app_name">My Application</string>
<string name="report_mkt">this is english text</string>
<string name="device_param_empty">this is english text</string>
Run Code Online (Sandbox Code Playgroud)

它在真实手机上的外观:

在此处输入图片说明

所以正如你所看到的,我的字符串也是英文的,一切正常,所以问题不在于英文字符串。