如何在android中显示通知内容文本中的图标和文本

如何在android中显示通知内容文本中的图标和文本,android,android-notifications,android-notification-bar,Android,Android Notifications,Android Notification Bar,我需要在我的android应用程序中显示一个通知 我正在使用以下代码: NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(baseContext).setLargeIcon(large_icon) .setSmallIcon(R.drawable.message_received_small_icon) .setContentIntent(pend

我需要在我的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”作为标题,下面显示“Photo”

但是,我不想只显示“photo”字符串,我想显示摄像头图标+“photo”字符串。 我没有找到任何方法在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;
    }

您可以将
remoteview
与自定义布局一起使用

请注意,安卓7牛轧糖在通知中有一些变化(我还没有深入讨论)。以下代码表示以下牛轧糖

// for standard notification
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_simple);

// for expanded notification
// RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.notification_expanded);

views.setImageViewBitmap(R.id.picture, YOUR_IMAGE_BITMAP_HERE);
views.setTextViewText(R.id.text, YOUR_TEXT_HERE);

NotificationCompat.Builder notificationBuilder = 
        new NotificationCompat.Builder(this)
                .setSmallIcon(YOUR_NOTIFICATION_ICON)
                .setCustomContentView(views);
                // .setCustomBigContentView(bigViews); // if you've set it

notification = notificationBuilder.build();

为通知创建自定义布局我建议您给通知一次机会()