【问题标题】:notification status bar icon doesn't change to white通知状态栏图标不会变为白色
【发布时间】:2019-03-25 09:12:59
【问题描述】:

正如标题所说,当我启动时,我通知状态栏上的小图标不会将颜色变为白色并且几乎不可见:

Notification n  = new Notification.Builder(this)
        .setContentTitle("title")
        .setContentText("lorem ipsum dolor sit amet")
        .setSmallIcon((R.drawable.logo_ntf))
        .setLargeIcon(icon)
        .setAutoCancel(true)
        //.addAction(R.drawable.transparent, null, null)
        .build();

【问题讨论】:

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


    【解决方案1】:

    对于Android 5+,您必须为通知小图标创建一个透明图标。

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notification.setSmallIcon(R.drawable.icon_transperent);
        notification.setColor(getResources().getColor(R.color.notification_color));
    } else { 
        notification.setSmallIcon(R.drawable.icon);
    }
    

    查看this了解更多信息

    【讨论】:

      【解决方案2】:

      原因:对于 5.0 Lollipop“通知图标必须全白”。

      如果我们通过将目标 SDK 设置为 20 来解决白色图标问题,我们的应用将不会以 Android Lollipop 为目标,这意味着我们无法使用 Lollipop 特定的功能。

      对于 Lollipop OS 版本以下和以上版本的 Notification Builder 的实现将是:

      Notification notification = new NotificationCompat.Builder(this);
      if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          notification.setSmallIcon(R.drawable.icon_transperent);
          notification.setColor(getResources().getColor(R.color.notification_color));
      } else { 
          notification.setSmallIcon(R.drawable.icon);
      } 
      

      reference link 并阅读文档5.0 Behavior Changes

      【讨论】:

        【解决方案3】:

        确保可绘制文件的透明度后......

        尝试将以下内容添加到 Notification.Builder 管道:

        .setColor(color);
        

        color 应该是一个资源 int 值,指的是一种颜色,如下所示:

        int color = getResources().getColor(R.color.notification_color);
        

        不推荐使用的方式:

        int color = ContextCompat.getColor(context, R.color.notification_color);
        

        来源:setColor documentation

        【讨论】:

        • 请确保发布所有必要的代码。请发布 R.drawable.logo_ntf 的代码作为您问题的更新。
        • 与其他任何颜色的图标一起工作.. 真的很奇怪
        • 好的,它可以工作。我只是将图像转换为黑白增加锐化。可能会出现此问题,因为图像包含渐变或由多种颜色组成。
        • 很高兴听到这个消息。
        • @GiorgioCafiso 如果这有助于解决问题,请随时设置答案。