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

Android 如何强制包含ltr字符的textView在rtl环境中右对齐,android,android-layout,right-to-left,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_

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

<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>

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

  • 我试着使用android:gravity=“center_vertical | start”(虽然“start”是默认设置),但没有任何帮助

  • 我试图添加android:textAlignment=“textStart”,但没有效果


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

您可以使用
android:textDirection=“rtl”
因此,无论文本是希伯来语还是英语,它都将从视图的右侧开始,并且在更改语言时不会跳转到其他方向

注意-我认为
android:gravity
不适合您,因为希伯来语是rtl,英语是ltr。
所以当您将gravity-start和语言设置为英语时,您的文本将从视图的左侧开始

使用工作示例进行编辑:

布局:

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" />
<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>
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\u width=“匹配父项”
android:layout\u height=“match\u parent”
工具:context=“.MainActivity”>

字符串资源:

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" />
<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>

我的申请
这是英文文本
这是英文文本

它在真实手机上的外观:

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" />
<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>

正如你所看到的,我也用英语输入字符串,一切正常,所以问题不在于有英语字符串。

使用

android:textAlignment="viewEnd"
文本将对齐,而不管其内容如何,但仍取决于布局方向

文件:

对齐到视图的末端,如果视图是 解析的布局方向为LTR,否则为左对齐


如果您的系统语言是RTL,则可以使用以下命令强制ltr语言与RTL对齐

android:textDirection="locale"

它确实有效。但这个解决方案只有在我的语言是RTL时才有效。[我可以用代码测试:if isRTL然后textView.setTextDirection,但我更喜欢一个适用于所有本地化的解决方案]但我已经用希伯来语和英语对此进行了测试,在这两次测试中,文本都是从视图的右侧开始的-我认为我的解决方案对您有效嘿@Atara,我也有英语字符串,并且它是右对齐的-我编辑了我的答案,请随意检查,并告诉我,如果您的问题没有用我编辑的答案解决。@Atara哦,好的,现在我理解你的问题-因此,是的,我的解决方案不会完全涵盖你的情况。正如你所说,它只涵盖1个方面-如果你可以在不使用java的情况下找到解决方案,请在此处进行评论(你也让我感兴趣),我会为RTL语言环境添加单独的布局XML,即使区别只是几个textDirection属性。