【发布时间】:2018-01-18 08:27:41
【问题描述】:
如何在通知(本地通知)正文的末尾添加小图像(图标)。 我正在使用 NotificationCompat.Builder 创建通知。我想在通知正文的末尾添加小图标。
【问题讨论】:
标签: android android-notifications android-bitmap
如何在通知(本地通知)正文的末尾添加小图像(图标)。 我正在使用 NotificationCompat.Builder 创建通知。我想在通知正文的末尾添加小图标。
【问题讨论】:
标签: android android-notifications android-bitmap
我认为你可以使用spannable String,因为setContentText(CharSequence message) 接受CharSequence。
String msg="Hello world";
SpannableStringBuilder ssb = new SpannableStringBuilder(msg);
ssb.setSpan(new ImageSpan(this, R.drawable.app_icon), msg.length()-1, msg.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
并将其添加到通知生成器中的消息中。
NotificationCompat.Builder notification
= new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_icon)
.setStyle(new NotificationCompat.BigTextStyle().bigText(ssb))
.setContentTitle("Hello")
.setContentText(ssb);
我没有测试过。但我认为它应该工作。注意索引。
【讨论】:
CharSequence 一起使用,但它很奇怪。谢谢澄清。您应该继续使用RemoteView。谢谢
您可以使用NotificationCompat.Builder#setCustomContentView(RemoteViews)。在这个related question 你可以看到一个例子。
考虑到通知模板在 Android N 中发生了变化,如果你走那条路,通常用户期望一个一致的 UI。见Android N: Introducing upgraded Notifications
【讨论】: