【问题标题】:Why do icons set with Notification.Builder.setSmallIcon in Android Lollipop show as a white square?为什么 Android Lollipop 中使用 Notification.Builder.setSmallIcon 设置的图标显示为白色方块?
【发布时间】:2014-11-28 12:28:02
【问题描述】:

我有这个代码:

Notification notif;

// Build notification
Notification.Builder notifBuilder = new Notification.Builder(context);
notifBuilder.setContentIntent(pendingIntent);
notifBuilder.setContentTitle(title);
notifBuilder.setSmallIcon(icon_resId);
notifBuilder.setContentText(ne.getCaption());
notifBuilder.setDefaults(Notification.DEFAULT_ALL);
notifBuilder.setAutoCancel(autocancel);
notifBuilder.setWhen(System.currentTimeMillis());
notif = notifBuilder.build();

在 Android 4.4 中运行良好。

但是,在 Android 5.0 中,状态栏中显示的图标是一个白色方块。设备锁定时出现的新“通知正文”中显示的图标是正确的。

http://developer.android.com/reference/android/app/Notification.Builder.html 中,我没有看到有关 API 级别 21 中通知图标的任何新内容

【问题讨论】:

标签: android notifications android-5.0-lollipop android-notification-bar


【解决方案1】:

查看文档: http://developer.android.com/design/style/iconography.html

有一句话:“通知图标必须全白。此外,系统可能会缩小和/或使图标变暗。”

【讨论】:

【解决方案2】:

我已解决将图标大小更改为 16x16 像素并仅使用白色

【讨论】:

  • 您没有注意到棒棒糖中的所有通知都被白色屏蔽了吗?为了在棒棒糖中使用好的图标,您应该使用具有明显形状的图标。所有颜色都将转换为白色,因此必须具有透明度。
  • 图片的完整资产大小应为 72*72,在 66 * 66 的光学正方形内才能正确显示。请参考petrnohejl.github.io/Android-Cheatsheet-For-Graphic-Designers
  • @SavageKing 这是一个很棒的资源,但尚未针对 Lollipop 进行更新。特别是,与 Google 应用程序相比,通知图标大小不再正确缩放。据我所知,他们在 xxxhdpi 的 96x96 图像中使用了 80x80 的图标。
  • 只使用白色是什么意思?所以你的小图标只是一个白色方块还是什么????
【解决方案3】:

如 Android 开发者网站通知下的 Android 5.0 行为更改中所述:

通知在白色(或非常浅的)背景上使用深色文本绘制,以匹配新的材料设计小部件。确保您的所有通知都与新的配色方案相匹配。如果您的通知看起来有问题,请修复它们:

使用 setColor() 在图标图像后面的圆圈中设置强调色。 更新或删除涉及颜色的资产。系统会忽略操作图标和主通知图标中的所有非 Alpha 通道。您应该假设这些图标仅是 Alpha 版。系统以白色绘制通知图标,以深灰色绘制操作图标。

http://developer.android.com/about/versions/android-5.0-changes.html.

【讨论】:

    【解决方案4】:

    重复:Notification bar icon turns white in Android 5 Lollipop

    简介:

    Android 5 更新: https://developer.android.com/about/versions/android-5.0-changes.html 通知 -> 材料设计风格

    更新或删除涉及颜色的资产。系统忽略所有 操作图标和主通知图标中的非 alpha 通道。 您应该假设这些图标仅是 Alpha 版。系统 以白色绘制通知图标,以深灰色绘制操作图标。

    可以使用(默认为灰色)设置小图标背景颜色:

    Notification.Builder#setColor(int)
    

    【讨论】:

      【解决方案5】:

      在你的清单中添加这个 -

       <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
          android:resource="@drawable/ic_notification" />
      

      【讨论】:

        【解决方案6】:

        任何人还在看这个,让你的图标正确显示的最简单方法是首先在这里用 Android Icon Studio 渲染它:

        https://romannurik.github.io/AndroidAssetStudio/icons-notification.html

        将下载的 zip 文件中的文件解压缩到您的项目 /main 文件夹中,以便它们插入相关的 drawable-xxxx 文件夹中。

        然后,要更改通知中的颜色,请使用以下内容:

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification_appicon) // <-- Icon from Android Icon Studio 
            .setColor(context.getColor(R.color.holo_blue))    // <-- Set your preferred icon colour to appear in the notification dropdown list
            .setContentTitle("Title")
            .setContentText("Content")
            .setAutoCancel(true)
            .setCategory(NotificationCompat.CATEGORY_EVENT)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        

        【讨论】:

          【解决方案7】:

          在 Android 5.0 中,状态栏中显示的图标是一个白色方块,因为 5.0 Lollipop “通知图标必须全白”

          您可以在 Material 图标上轻松找到此类图标。 访问:https://material.io/icons/

          Google 还建议我们使用自定义颜色,使用setColor() 方法显示在白色通知图标后面。

          欲了解更多信息,请访问:https://developer.android.com/about/versions/android-5.0-changes.html

          【讨论】:

            【解决方案8】:

            manifest.xml 中删除android:targetSdkVersion="21"。 它会工作的!

            【讨论】:

            • 这不是一个解决方案,它是一种违背系统的坏方法。
            • manifest文件中有android:targetSdkVersion="21" 版本的解决方案吗?
            • @john,不,但设置 android:targetSdkVersion="19" 也很有效。对其他人:我不太明白这个答案的不喜欢。问题基本上是“棒棒糖不想按照我想做的方式做事”。因此,从逻辑上讲,您的选择是接受棒棒糖方式,或者不以棒棒糖为目标。
            • 为什么 targetSdkVersion 首先在 manifest.xml 中?这些东西应该在 build.gradle 中设置。
            • 此解决方案不起作用。甚至在 sdk ver 21 或更高版本中停止通知。