我在 TextView 中使用了两部分,第一部分是日期,另一部分是姓名和电子邮件。 它们都在同一个 TextView 中被引用。我想更改日期的颜色,使其具有与姓名和电子邮件不同的视觉效果。是否可以在不实际为名称和电子邮件添加全新的 TextView 的情况下执行此操作? 到目前为止,这是我的代码:
String nameandemail;
holder.mytext.setText(String.valueOf(dateFormat.format(new Date(msg.getDate())) + " " + nameandemail + ": "));
如何设置日期颜色
holder.mytext.setTextColor(Color.white)
nameandemail 字符串是绿色的吗?
谢谢!
最佳答案
您可以使用 spans .
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
// Set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// Set the text color for first 6 characters
sb.setSpan(fcs, 0, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
你也可以像下面这样使用html
myTextView.setText(Html.fromHtml(text + "<font color=white>" + some_text + "</font><br><br>"
+ some_text));
关于android - 如何更改 TextView 特定部分的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33246735/