我正在Android上开发一个小聊天应用程序,我希望每当发送消息时都会将用户名着色,所以基本上我有一个显示消息的TextView
outputView.append("\n"+event.getSender()+": "+event.getMessage());
现在我有了,问题是我有一个TextView,它在一个地方显示用户名和消息。如何为用户名event.getSender()
谢谢!
编辑:正如NKN告诉我的那样,我将其更改为此。然而,我仍然无法让它工作,它只是崩溃......我写错了什么?
final SpannableStringBuilder sb = new SpannableStringBuilder(event.getSender());
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to set text color to some RGB value
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// Span to make text bold
sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// Set the text color for first 4 characters
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// make them also bold
outputView.setText("\n"+sb+": "+event.getMessage());
答案 0 :(得分:0)
您可以使用Html
课程,例如:
tv.setText(Html.fromHtml("<font color="#FF0000">This is a red text</font> and this one is <b>bold</b>");
此外,您也可以使用SpannableStringBuilder
,我建议您阅读此link