39

I used this simple code to set a Notification in Android 4.1 or higher. It works well, but my problem comes with SmallIcon and LargeIcon. I understand that SmallIcon is shown in the status bar and the LargeIcon is shown in the dropdown list.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("The ticker");
builder.setContentTitle("The title");
builder.setContentText("The text");
builder.setSmallIcon(R.drawable.my_small_icon);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.my_96px_large_icon);
builder.setLargeIcon(bm);       
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify("direct_tag", NOTIF_ALERTA_ID, builder.build());

My problem is:

  1. When the notification is launched, a cropped oversized Small Icon is shown next to "The Ticker" text, instead of showing the original SmallIcon without oversizing it. enter image description here

  2. In the dropdown list I see the LargeIcon on the left, that's good. But I also see the small icon on the right, next to the time of the notification. I don't want to show that. enter image description here

4

5 回答 5

30
  1. 在我的应用程序中,我提供了可绘制的大 (128x128 像素) PNG 作为小图标,它显示为缩放且没有裁剪。您的可绘制对象是在位图文件中定义的还是作为 XML 资源定义的?在 XML 中,您可以指定显示的几个方面(例如裁剪)。仔细检查您的 XML 或仅使用 PNG/JPG。

  2. 正如Notification.setSmallIcon() 上的 Android API 文档明确指出的那样:

    设置小图标资源,用于表示状态栏中的通知。展开视图的平台模板将在左侧绘制此图标,除非还指定了大图标,在这种情况下,小图标将移动到右侧

AFAIK 除非您提供自己的通知模板(通过Notification.setContent()

于 2013-01-11T14:48:10.957 回答
14

有一种方法可以解决这个奇怪的实现。而不是setLargeIcon使用这个:

Notification notification=notificationBuilder.build()
notification.contentView.setImageViewResource(android.R.id.icon, R.drawable.your_large_icon);
于 2014-12-27T04:51:50.740 回答
8

我猜这是预期的行为。

您应该检查您的小图标是否遵循图标大小的 UX 指南。小图标限制为 24x24dp。

扩展通知的默认行为是同时显示大图标和小图标。我不确定有没有办法摆脱小图标,但为什么这很重要?

于 2012-12-12T20:18:29.810 回答
1

就我而言,我只是将红色图标设置为大图标,将 setColor 设置为 Color.WHITE,并将白色图标设置为小图标。这样,在通知区域中,我的红色图标就会显示出来,而白色图标会“消失”。

Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.app_logo);

mBuilder.setContentIntent(resultPendingIntent).setColor(Color.WHITE).setLargeIcon(icon);
于 2017-06-01T19:44:47.147 回答
-1

就我而言,我没有在所有文件夹(xhdpi、hdpi、mdpi、ldpi)中放置图标图像。

于 2013-12-21T13:58:04.630 回答