Android从展开的通知布局中删除smallIcon叠加层

时间:2016-07-24 11:28:26

标签: android android-notifications

我试图在Lollipop上显示扩展布局的通知,但我不希望smallIcon显示在largeIcon的右下角,如下例所示。请注意,这只发生在扩展布局上(我尝试了提出的解决方案here,但它似乎不起作用)。 enter image description here

这是代码:

NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
notification.setContentTitle(reader.getString(Constants.NOTIFICATION_CONFIG_KEY_TITLE));
notification.setContentText(reader.getString(Constants.NOTIFICATION_CONFIG_KEY_TEXT)) ;
notification.setAutoCancel(true) ;
notification.setSmallIcon(Constants.NOTIFICATION_CONFIG_ICON_ID);
notification.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), Constants.NOTIFICATION_CONFIG_ICON_ID));
Notification n = notification.build() ;

一旦建立通知,smallIcon的可见性可能会被更改,但我不清楚如何。也许,有人可以提供帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以通过自己构建两种布局(短版和展开版)来实现这一点。

首先制作 2 个 XML 文件并自定义您的布局,然后将它们附加到通知中。 这些布局的根视图应该是 LinearLayout

RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout); // the small layout

RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_layout_large); // the expand layout

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
                .setSmallIcon(R.drawable.logo)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded);


NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());

如果您想为这些布局动态设置文本或图像(不在 XML 中),您可以这样做:

RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout);
        
String text = "the text to display";
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.notification_default_img);;


notificationLayout.setTextViewText(R.id.small_notification_text, text); 
notificationLayout.setImageViewBitmap(R.id.small_notification_img, picture);