我目前有一个通知,显示发送者和他们发送的消息。我想将名字加粗显示,但是我不知道怎么做。有人知道吗?
Html.fromHtml(String.format(Locale.getDefault(), "<b>%s</b>", wrappedName))
这是我目前正在使用但不起作用的东西。
Spannable sb = new SpannableString("Bold text");
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Notification notification = new Notification.Builder()
.setContentTitle("5 New mails from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new Notification.InboxStyle()
.addLine(str1)
.addLine(sb);
.setContentTitle("")
.setSummaryText("+3 more"))
.build();
解决方案
只需使用以下代码即可在通知中获得加粗的文本。
Html.fromHtml(String.format(Locale.getDefault(), "<strong>%s</strong>", wrappedName));
<b>
是旧版的HTML标签。使用<strong>
作为加粗文本的新版HTML标签(HTML5)。
有一种更好的方法可以实现此目的,而不需要使用formHtml
,请参考:样式化通知InboxStyle和这位安卓开发者。
"<strong>%s</strong>"
吗? - Turtle