代码动态添加文字

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值