如何将如此大的图标添加到Android通知中?

时间:2018-12-06 12:33:42

标签: android android-notifications

我有这样的最新Android P通知设计 enter image description here

对于此通知,我需要在右侧添加蓝色用户图标。你能告诉我如何实现吗?是否可以使用NotificationBuilder的setter方法之一进行制作?我需要为此通知编写自定义布局吗?

2 个答案:

答案 0 :(得分:0)

您必须使用自定义布局进行通知以覆盖默认配置。

  1. 使用NotificationCompat.Builder构建基本通知。
  2. 调用setStyle(),并向其传递NotificationCompat.DecoratedMediaCustomViewStyle的实例。
  3. 将自定义布局扩展为RemoteViews的实例。
  4. 调用setCustomContentView()设置折叠通知的布局。

(可选)还调用setCustomBigContentView()为展开的通知设置不同的布局。

示例:

// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), 
R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), 
R.layout.notification_large);

// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(notificationLayout)
    .setCustomBigContentView(notificationLayoutExpanded)
    .build();

关注this以获取详细信息

答案 1 :(得分:0)

在您的构建器上添加此行

.setLargeIcon(BitmapFactory.decodeResource(context.resources , R.drawable.your_image))