当背景色为白色时,安卓通知图标颜色未变化

20

我升级了Android 6.0,但我的应用程序出现了问题。当状态栏背景颜色不是白色时,通知图标看起来很好。 (通知图标PNG只有白色和透明度) 但是,如果某些应用程序将背景颜色更改为白色,则我的通知图标不会反转为黑色。如何在其他应用程序将状态栏背景颜色设置为白色时将白色通知图标反转为黑色? (我不是在问如何使用彩色图标。) 下面的图片显示了一个问题。

正常状态 normal status image

当背景颜色更改为白色时,只有我的图标没有变黑 with white background color image

  • 通知构建代码:

      Notification.Builder mBuilder =
              new Notification.Builder(context)
                      .setSmallIcon(R.drawable.ic_notifications_none)
                      .setPriority(priority2)
                      .setOngoing(true);
    
      mBuilder.setContent(generateMessageView(message));
    
      Intent notificationIntent = new Intent(context, MainActivity.class);
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
                                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
      PendingIntent intent = PendingIntent.getActivity(context, 0,
              notificationIntent, 0);
      NotificationManager mNotificationManager =
              (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      mBuilder.setContentIntent(intent);
      mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    
  • values-v23/styles.xml

    ->

    values-v23/styles.xml

  •   <style name="AppTheme" parent="android:Theme.Material.NoActionBar">            
      </style>
    

找到解决方案

我将通知图标添加到drawable目录而不是drawable-*dpi目录中。现在它可以正常工作了。


请查看此帖子。同时阅读此文档,希望您能得到一些线索。干杯.... :) - Gypsy
@verbose 我已经阅读了,但会再次检查。谢谢。 - Jiyoung Kim
我尝试了你的解决方案,但对我没有起作用。 - Cerlin
希望您考虑将问题中的解决方案复制到答案中,因为它解决了我的问题,而这个问题的任何答案都没有,但我碰巧看到了您写的内容。我本可以只是路过,找不到合适的答案,然后关闭您的问题,尽管它已经包含了我所需的内容。 - Omar Shawky
2个回答

19

虽然回答有些晚了,但对于其他遇到同样问题的人,

我也遇到过这个问题,我发现问题出在图标上。 你可以使用这个在线工具来解决问题。 打开这个链接:

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_stat_call_white

选择您的图像(具有大尺寸),下载资源并将其复制到您的项目中。
最后使用.setSmallIcon(R.drawable.ICON_NEW_NAME)设置通知图标。
希望这可以帮助到您。

在Android 8版本中,它显示为深灰色的正方形框。 - Kumararaja

1

我认为问题出在安卓5.0或更高版本的设备上。

https://developer.android.com/design/patterns/notifications.html
https://developer.android.com/about/versions/android-5.0-changes.html

这里是一个解决方案:
Notification notification = new Notification.Builder(context)
        .setAutoCancel(true)
        .setContentTitle("My notification")
        .setContentText("Look, white in Lollipop, else color!")
        .setSmallIcon(getNotificationIcon())
        .build();

return notification;

并且方法 getNotificationIcon()

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.icon_black : R.drawable.ic_nomarl;
}

谢谢你,但它没有起作用。我从Android示例中找到了LNotifications,但当其他应用程序在白色状态栏下运行时,它的图标是白色变黑色(深灰色),而不是黑色。我没有找到任何特殊的代码来改变LNotifications的颜色。 - Jiyoung Kim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接