我有一个支持RTL的应用程序。当格式化两队之间的分数我写的线沿线的东西:
t1.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
如果我的文本字段被设置为textDirection =“区域设置”,执行Android 8和Android 9,在我的华为所有我的测试的设备和基本上到处Android模拟器 - 其输出“1 0”。但是,如果我测试这对三星S7采用Android 8.0则返回“1 - 0”。我既是一个物理设备上和三星的设备实验室测试:https://developer.samsung.com/remotetestlab/rtlDeviceList.action
这是这样做的错误的方式?我怎样才能格式化比分因此它可以在两个LTR和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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home team won 1-0"/>
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutDirection="locale"
android:text="1-0"
/>
<TextView
android:id="@+id/t2"
android:layoutDirection="rtl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:id="@+id/t3"
android:textDirection="locale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:textDirection="rtl"
android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:textDirection="rtl"
android:id="@+id/t5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:textDirection="locale"
android:id="@+id/t6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:textDirection="locale"
android:id="@+id/t7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:textDirection="locale"
android:id="@+id/t8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
<TextView
android:textDirection="locale"
android:id="@+id/t9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-0"
/>
</LinearLayout>
应用程序的代码:
t1.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
t2.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
t3.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
t4.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
t5.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
t6.setText(BidiFormatter.getInstance(Locale.forLanguageTag("ar-MA")).unicodeWrap(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0), TextDirectionHeuristicsCompat.ANYRTL_LTR));
t7.setText(BidiFormatter.getInstance(Locale.forLanguageTag("ar-MA")).unicodeWrap(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0), TextDirectionHeuristicsCompat.RTL));
t8.setText("\u200F" + String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
t9.setText("ع" + String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));
setSupportActionBar(toolbar);
看来三星设备这个bug /功能可以在我的情况下,有一种变通方法:
String RTL_INDICATOR = "\u200f";
t1.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d %s- %d", 1, RTL_INDICATOR, 0));
这从右到左标记(https://www.fileformat.info/info/unicode/char/200f/index.htm)在两个整数中间插入。如果在该字符串的开头插入它并没有解决问题。
该代码变得很丑陋,因为我必须这样做在很多地方,但它适用于所有的Android版本的所有设备(即我测试过),所以这标志着作为答案现在。
加
android:layoutDirection="rtl"
您
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"