Ошибка при форматировании «% d -% d» в RTL на устройствах Samsung с Android 8.0 - PullRequest
0 голосов
/ 06 февраля 2019

У меня есть приложение, которое поддерживает RTL.При форматировании партитуры между двумя командами я пишу что-то вроде:

t1.setText(String.format(Locale.forLanguageTag("ar-MA"), "%d - %d", 1, 0));

Что выводит «0 - 1» в эмуляторе Android, если для моего текстового поля установлено значение textDirection = "locale", на всех моихТестовые устройства под управлением Android 8 и Android 9, на моем Huawei и в основном везде.Но если я протестирую это на Samsung S7 с Android 8.0, он возвращает «1 - 0».Я тестировал как на физическом устройстве, так и на лаборатории устройств от Samsung: https://developer.samsung.com/remotetestlab/rtlDeviceList.action

Это неправильный способ сделать это?Как я могу отформатировать партитуру, чтобы она работала как на LTR, так и на RTL на всех устройствах, когда у меня есть одно текстовое поле, содержащее партитуру?Это прекрасно работает на большинстве устройств, но мне кажется, что что-то не так в отношении причин, по которым оно работает на устройствах Samsung.

Вот мой код:

<?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);

Скриншот эмулятора: enter image description here

Скриншот Samsung S7: enter image description here

Ответы [ 2 ]

0 голосов
/ 06 февраля 2019

Кажется, что эта ошибка / функция на устройствах Samsung в моем случае может иметь обходной путь:

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 (которые я тестировал), поэтому отметьте это как ответ.

0 голосов
/ 06 февраля 2019

добавьте

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