0

你好朋友我在单个文本视图中设置了两种不同的颜色,如下所示 输出画面

我的代码如下

public static String getAnswers(List<Question> questions) {
    int question = 1;
    StringBuffer sb = new StringBuffer();
    for (Question q : questions){
        String d= "<font color=#292929>" +   sb.append("Q").append(question).append(") ").append(q.getQuestion()).append("? \n")+"</font>";
        String ss="<font color=#ce4e00>"+sb.append("Answer: ").append(q.getAnswer()).append("\n\n")+"</font>";

            AnswersActivity.results.setText(Html.fromHtml(d+ss),TextView.BufferType.SPANNABLE);
        question ++;
    }

    return sb.toString();
}

是我设置的textview

        mStringQustion = Utility.getAnswers(currentGame.getQuestions());

并获得如下输出

输出图像

我该如何解决?

4

2 回答 2

2

我遇到了同样的问题,我像下面一样修复了它

你应该使用ForegroundColorSpan

String firstString = "Book Title:";
String secondString = "Book Description";
String thirdString = "1.SubTitle";

        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                firstString + "\n" + secondString + "\n" + thirdString);
        stringBuilder.setSpan(new RelativeSizeSpan(2.0f), 0,
                firstString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        stringBuilder.setSpan(new StyleSpan(
                android.graphics.Typeface.BOLD_ITALIC), firstString.length(),
                firstString.length() + secondString.length() + 1,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        stringBuilder.setSpan(
                new ForegroundColorSpan(Color.rgb(255, 0, 0)),
                firstString.length() + secondString.length() + 1,
                firstString.length() + secondString.length()
                        + thirdString.length() + 2,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        AnswersActivity.results.setText.setText(stringBuilder);

在此处输入图像描述

您可以根据您的文字进行更改。

希望这会帮助你。

于 2013-10-11T12:53:43.950 回答
0

像这样试试

String d = "&lt;![CDATA[ &lt;font color=#292929>" + sb.append("Q").append(question).append(")").append(q.getQuestion()).append("? \n")+"</font>";  
String ss="<font color=#ce4e00>" + sb.append("Answer:").append(q.getAnswer()).append("\n\n")+"</font>]]>";
于 2013-10-11T12:27:49.557 回答