在RTL字符串中间使用LTR文本时TextView中的文本方向改变

问题描述 投票:7回答:1

我们的应用程序支持其他本地人。中间有一个带有两个可替换值的字符串(带货币符号的货币USD $ XXXXX)。但是,当语言环境为阿拉伯语时,会有奇怪的行为。当文本多于一行时,文本的方向会改变。而且只有第一行的文本是正确的,而其他行的格式却被某些东西覆盖!

如您在屏幕快照中所见,绿线符合预期,而红线则是错误的。

enter image description here

到目前为止,我已经尝试使用:

  • BidFormatter
  • Unicode

问题在于,使用比迪格式后,第一个数字正确,但第二个数字不正确。

并且在使用BidiFormat和unicode后,所有数字都很好,但是当文本较长且变成多行时,只有第一行是正确的,其他行又是错误的。

对于unicode,我看了一下:Unicode® Standard Annex #9 UNICODE BIDIRECTIONAL ALGORITHM(如果您只对主要内容感兴趣,可以直接see this part

您可以查看此仓库:Github Link

这是我用于快速参考的代码:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setTextViewTexts(R.id.tvLtrOneLine, formatWithCurrency(), R.string.text_short_english)
    setTextViewTexts(R.id.tvLtrTwoLines, formatWithCurrency(), R.string.text_long_english)
    setTextViewTexts(R.id.tvRtlOneLine, formatWithCurrency(), R.string.text_short_arabic)
    setTextViewTexts(R.id.tvRtlOneLineBidi, bidiFormatter(formatWithCurrency()), R.string.text_short_arabic)
    setTextViewTexts(R.id.tvRtlOneLineRtlFormatter, rtlMaker(formatWithCurrency()), R.string.text_short_arabic)
    setTextViewTexts(R.id.tvRtlTwoLines, formatWithCurrency(), R.string.text_long_arabic)
    setTextViewTexts(R.id.tvRtlTwoLinesBidi, bidiFormatter(formatWithCurrency()), R.string.text_long_arabic)
    setTextViewTexts(R.id.tvRtlTwoLinesRtlFormatter, rtlMaker(formatWithCurrency()), R.string.text_long_arabic)
}

private fun setTextViewTexts(textViewId: Int, text: String, stringResource: Int) {
    findViewById<TextView>(textViewId).text = getString(stringResource, text, text)
}

private fun formatWithCurrency(): String {
    val currency = "USD$"
    val price = 200
    val priceBuilder = StringBuilder("")
    priceBuilder.append(currency)
    priceBuilder.append(getDecimalFormattedPrice(price))
    return priceBuilder.toString()
}

private fun getDecimalFormattedPrice(price: Int): String {
    return DecimalFormat("0.00").format(price)
}

private fun rtlMaker(text: String): String {
    return "\u2066" + bidiFormatter(text) + "\u2069"
}

private fun bidiFormatter(text: String): String {
    return BidiFormatter.getInstance().unicodeWrap(text)
}

这是一个Android错误还是有解决方法?

要查看错误,请下载资源库并在任何设备上运行并将设备语言更改为阿拉伯语(埃及)

编辑:我提交bug report

android textview right-to-left bidi
1个回答
1
投票

@您可以使用Omid HeshmatiniaspannableString要么Html.fromHtml(string)它会为您提供更准确的输出:)

© www.soinside.com 2019 - 2024. All rights reserved.