我需要在我的 Android 应用中显示通知。
我正在使用以下代码:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(baseContext).setLargeIcon(large_icon)
.setSmallIcon(R.drawable.message_received_small_icon)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSound(soundUri);
.setContentTitle("New Message");
.setContentText("video");
这个通知显示在左侧的“消息接收”图标。在接收信息图标的右侧,它以“DHan Nexus”为标题,并在其下方显示“照片”。
但是我想显示相机图标+“照片”字符串,而不仅仅是显示“照片”字符串。我没有找到在setContentText() API中显示相机图标的方法,请问如何实现?我需要制作一个自定义布局吗,还是默认方法就可以?
以下是图片,使问题更加明确:
我尝试使用SpannableString来显示图标和文本,但似乎不起作用。
CharSequence cs = getContentIcon(text);
.setContentText(cs.toString());
private CharSequence getContentIcon(String text)
{
Drawable image = ContextCompat.getDrawable(baseContext, R.drawable.camera_icon);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
SpannableString sb = new SpannableString(" "+text);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
sb.setSpan(imageSpan, 0, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
return sb;
}