问题
Numbers inside TextView
are getting reversed when formatted in RTL.
When numbers are at the end of a text inside a TextView
they getting reversed. How can I solve this programmatically?
As an example, the numbers below are reversed:
They should be displayed like:
回答1:
The misunderstand: Digits in RTL languages like ARABIC should be written from RTL with the arabic digits to avoid any problems i.e: "تم إرسال رسالة نصية للرقم ١٢٣٤" Note that I wrote "رسالة نصية" NOT "SMS رسالة".
The problem and it's solution: Mixing more than one direction languages required more steps, you need to tell the system "hey this is RTL word, add as it to sequence". So you may need to do this implicitly, i.e:
\u200f + تم إرسال رسالة نصية إلى + number
Consider StringBuilder: It's very painful for developer to develop something for RTL language using plus(+) notation, this much confusing and error prone.
A better way:
builder.append("\u061C").append(" تم إرسال رسالة نصية لـ").append("\u200E").append("+0123456789")
Consider BidiFormatter: Utility class for formatting text for display in a potentially opposite-directionality context without garbling
Example:
String text = "{0} تم إرسال رسالة نصية لـ ";
String phone = BidiFormatter.getInstance().unicodeWrap("+961 01 234 567");
String result = MessageFormat.format(text,phone);
Now, result
will be formatted properly.
More examples on how BidiFormatter work.
回答2:
Try this out
android:supportsRtl="false" in manifest file
and android:gravity="start" in your layout.
回答3:
set the textview gravity to start
android:gravity="start"
来源:https://stackoverflow.com/questions/50624394/numbers-inside-textview-getting-reversed-when-formatted-in-rtl