String txt1 = getString(R.string.message_text2); String txt2 = getString(R.string.message_text3); String txt3 = getString(R.string.message_text4); String txt4 = getString(R.string.message_text5); String txt5 = getString(R.string.message_text6); String txt6 = getString(R.string.message_text7); String txt7 = getString(R.string.message_text8); String txt8 = getString(R.string.message_text9); String txt9 = getString(R.string.message_text10); String txt10 = getString(R.string.message_text11); String txt11 = getString(R.string.message_text12); /** * 方法一 */ SpannableStringBuilder builder0 = new SpannableStringBuilder(txt1); builder0.setSpan(new ForegroundColorSpan(Color.RED), 0, txt1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); SpannableStringBuilder builder1 = new SpannableStringBuilder(txt9); builder1.setSpan(new ForegroundColorSpan(Color.RED), 0, txt9.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder0.append("\n\t\t" + txt2).append("\n\t\t" + txt3) .append("\n\t\t" + txt4).append("\n\t\t" + txt5).append("\n\t\t" + txt6).append("\n\t\t" + txt7).append("\n\t\t" + txt8 + "\n\t\t"); builder1.append("\n\t\t" + txt10).append("\n\t\t" + txt11); text.append(builder0); text.append(builder1); /** * 方法二 */ SpannableString spannableString = new SpannableString(txt1); spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, txt1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); SpannableString spannableString1 = new SpannableString(txt9); spannableString1.setSpan(new ForegroundColorSpan(Color.RED), 0, txt9.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.append("\n\t\t" + spannableString); text.append("\n\t\t" + txt2 + "\n\t\t" + txt3 + "\n\t\t" + txt4 + "\n\t\t" + txt5 + "\n\t\t" + txt6 + "\n\t\t" + txt7 + "\n\t\t" + txt8 + "\n"); text.append(spannableString1); text.append("\n\t\t" + txt10 + "\n\t\t" + txt11);
代码动态添加文字
最新推荐文章于 2024-07-22 16:46:17 发布