android - spannable 字符串在不同的设备上给出不同的结果

标签 android string spannablestring

我正在使用 SpannableStringBuilder 在 TextView 上设置文本,但它在不同的设备上给我不同的结果。

这里是 spannableString 构建器的代码

 SpannableStringBuilder builder = new SpannableStringBuilder();
        SpannableString first = new SpannableString("Directions to Send or Clear Alert:\n\n" +
                "Option 1 of 2: To Send Alert, click the \"");
        first.setSpan(new ForegroundColorSpan(Color.BLACK), 0, first.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        first.setSpan( new StyleSpan(Typeface.BOLD), 0,  first.length()-27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableString second = new SpannableString("Match / STOP!");
        second.setSpan(new ForegroundColorSpan(Color.RED), 0, second.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        SpannableString third = new SpannableString("\" button next to the matching person.\n" +
                "Option 2 of 2: To Clear Alert, you can choose either of these 2 options:\n");
        third.setSpan(new ForegroundColorSpan(Color.BLACK), 0, third.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        third.setSpan( new StyleSpan(Typeface.BOLD), 38,  53, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableString third1 = new SpannableString("A) Click any of the \"");
        third1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, third1.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        third1.setSpan( new StyleSpan(Typeface.BOLD), 0,  1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableString four = new SpannableString("No Match / CONTINUE");
        four.setSpan(new ForegroundColorSpan(Color.parseColor("#008000")), 0, four.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        SpannableString five = new SpannableString("\" buttons.\n" +
                "B) Click on the \"X\" (clear) button at the top right of this message screen.");
        five.setSpan(new ForegroundColorSpan(Color.BLACK), 0, five.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        five.setSpan( new StyleSpan(Typeface.BOLD), 11,  12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        builder.append(first);
        builder.append(second);
        builder.append(third);
        builder.append(third1);
        builder.append(four);
        builder.append(five);

        headingTv.setText(builder);

在某些设备上,文本显示如下,这是正确的,

enter image description here

但在某些设备(如 google pixel2)中,它看起来像这样,这太可怕了。

enter image description here

任何人都可以帮我解决这个问题吗?

最佳答案

试一试

String first = "Directions to Send or Clear Alert:\n\n" + "Option 1 of 2: To Send Alert, click the \"";
String second = "Match / STOP!";
String third = "\" button next to the matching person.\n" + "Option 2 of 2: To Clear Alert, you can choose either of these 2 options:\n";
String third1 = "A) Click any of the \"";
String four = "No Match / CONTINUE";
String five = "\" buttons.\n" + "B) Click on the \"X\" (clear) button at the top right of this message screen.";


int blackColor = ContextCompat.getColor(mContext,R.color.black);
Spanned third1Spanned = setSpanColor(third1, blackColor);
headingTv.setText(TextUtils.concat(
        first,
        " " ,
        second,
        " " ,
        third,
        " " ,
        third1Spanned,
        " " ,
        four,
        " " ,
        five));


public static Spanned setSpanColor(String s, int color) {
    SpannableString ss = new SpannableString(s);
    ss.setSpan(new ForegroundColorSpan(color), 0, s.length(), 0);
    ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),0,s.length(),SPAN_EXCLUSIVE_EXCLUSIVE);
    return ss;
}

关于android - spannable 字符串在不同的设备上给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55215555/

相关文章:

java - 如何更改自定义 ListView 项目中的图像 - Android

android - 在 android 中处理 SpannableStringBuilder 图像

具有行间距的 Android spannable 字符串

android - WebView 显示某些链接的空白 View

java - Android Settings.ACTION_DISPLAY_SETTINGS 未找到

php - 从 php 网站检索数据到 Android 应用程序

regex - 从 r 中的字符串中提取时间

python - 为什么 list.reverse 不返回列表?

ios - Objective-c RegEx 变量名称($1、$2 等)

android - 将 ImageSpan 对齐到 TextView 的顶部