如何在通知中加粗文本

5

我目前有一个通知,显示发送者和他们发送的消息。我想将名字加粗显示,但是我不知道怎么做。有人知道吗?

Html.fromHtml(String.format(Locale.getDefault(), "<b>%s</b>", wrappedName))

这是我目前正在使用但不起作用的东西。


你尝试过 "<strong>%s</strong>" 吗? - Turtle
添加通知代码以更好地帮助您。 - Bruno Ferreira
3个回答

6
通常使用InboxStyle比HTML部分更好。
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();

4

解决方案

只需使用以下代码即可在通知中获得加粗的文本。

Html.fromHtml(String.format(Locale.getDefault(), "<strong>%s</strong>", wrappedName));

1

<b>是旧版的HTML标签。使用<strong>作为加粗文本的新版HTML标签(HTML5)。

有一种更好的方法可以实现此目的,而不需要使用formHtml,请参考:样式化通知InboxStyle这位安卓开发者


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接