在某些手机上,无法使用跨度字符串更改阿拉伯音调符号的颜色

时间:2018-07-08 07:45:24

标签: android spannablestring

我使用以下代码更改了阿拉伯音音符号的颜色:

List<String> arabicV = Arrays.asList("ؘ","ؙ","ؚ","ؐ","ؐؑ","ؒ","ؓ","ؔ","ؕ","ؖ","ؗ","ؗ","ﹰﹰ","ﹲ","ﹴ","ﹸ","ﹼ","ﹾ",
            "ٍ","ً","ُ","ِ","َ","ّ","ٓ","ٔ","ْ","ِ","َّ","َ","َْ","َ","ً","ٌ","َ","ُ","ٍ"
            ,"َ","ْ","ِ","ُ","ّ","ً");

    for (int j = 0; j < myString.length; j++) {
        if (arabicV.contains(myString.substring(j, j + 1))) {
             wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")),
                                        j, j+1 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                            }

                        }

像这样在我的设备上运行正常:

enter image description here

,但是在某些设备(例如LG-G5)上,它不起作用。像这样:

enter image description here

有人知道如何解决此问题或其他替代方法吗?

1 个答案:

答案 0 :(得分:1)

我找到了答案,有一种方法可以在所有设备上正确设置:

SpannedString result = new SpannedString("");
            for (int j=0;j<myString.length();j++){
                Spannable wordtoSpan = new SpannableString(myString.substring(j,j+1));
                if (arabicV.contains(myString.substring(j,j+1))) {
                    wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")),
                            0, 1, 0);

                }
                else
                {
                    wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor( "#000000")),
                            0, 1, 0);
                }
                result = (SpannedString) TextUtils.concat(result,"",wordtoSpan);
            }