我有一个应用程序显示自定义通知。问题是在Android 5中运行时,通知栏中的小图标显示为白色。我该如何解决这个问题?


当前回答

为了避免通知图标变成白色,使用“剪影”图标。透明背景图片。 你可以使用Irfanview来构建它们:

choose a picture, open in IrfanView, press F12 for the painting tools, clean picture if necessary (remove unwanted parts, smooth and polish) Image / Decrease Color Depth to 2 (for a black & white picture) Image / Negative (for a white on black picture) Image / Resize/Resample to 144 x 144 (use Size Method "Resize" not "Resample", otherwise picture is increased to 24 color bits per pixel (24 BPP) again File / Save as PNG, check Show option dialog, check Save Transparent Color, click Save, then click on the black color in the image to set the transparent color

Android似乎只使用drawablexxhdpi图片分辨率(144 x 144),所以复制你的结果ic_notification.png文件到\AndroidStudio\Projects\ app\src\main\res\ drawablexxhdpi。在代码中使用. setsmallicon (R.drawable.ic_notification),或者像Daniel Saidi上面建议的那样使用getNotificationIcon()。

你也可以使用Roman Nurik的Android Asset Studio。

其他回答

公认的答案并不(完全)正确。当然,它使通知图标显示颜色,但这样做有一个很大的缺点-通过设置目标SDK低于Android棒棒糖!

如果你通过将目标SDK设置为20来解决白色图标的问题,你的应用将不会针对Android Lollipop,这意味着你不能使用Lollipop特有的功能。

看看http://developer.android.com/design/style/iconography.html,你会看到白色风格的通知是如何显示在Android棒棒糖。

在Lollipop中,谷歌还建议您使用将显示在(白色)通知图标后面的颜色- https://developer.android.com/about/versions/android-5.0-changes.html

因此,我认为更好的解决方案是在应用程序中添加一个轮廓图标,并在设备运行Android Lollipop时使用它。

例如:

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_silhouette : R.drawable.ic_launcher;
}

通知是灰色的,如下所述。不管别人写了什么,它们都不是黑白分明的。你可能见过带有多种色调的图标,比如网络强度条。

在API 21 (Lollipop 5.0)之前,彩色图标可以工作。您可以强制应用程序以API 20为目标,但这会限制应用程序可用的特性,因此不建议这样做。您可以测试正在运行的API级别,并适当地设置颜色图标或灰度图标,但这可能不值得。在大多数情况下,最好使用灰色图标。

图像有四个通道,RGBA(红/绿/蓝/ alpha)。对于通知图标,Android忽略R、G和B通道。唯一重要的通道是Alpha,也称为不透明度。用编辑器设计图标,让您可以控制绘图颜色的Alpha值。

Alpha值如何生成灰度图像:

Alpha = 0(透明)-这些像素是透明的,显示背景颜色。 Alpha = 255(不透明)-这些像素是白色的。 Alpha = 1…254 -这些像素正是你所期望的,提供透明和白色之间的阴影。

使用setColor更改:

Call NotificationCompat.Builder.setColor(int argb). From the documentation for Notification.color: Accent color (an ARGB integer like the constants in Color) to be applied by the standard Style templates when presenting this notification. The current template design constructs a colorful header image by overlaying the icon image (stenciled in white) atop a field of this color. Alpha components are ignored. My testing with setColor shows that Alpha components are not ignored; instead, they still provide greyscale. Higher Alpha values turn a pixel white. Lower Alpha values turn a pixel to the background colour (black on my device) in the notification area, or to the specified colour in the pull-down notification. (It seems others have reported slightly different behavior, so be aware!)

根据Android设计指南,你必须为builder.setSmallIcon(R.drawable.some_notification_icon);但如果你仍然想显示一个彩色的图标作为通知图标这里是窍门棒棒糖和以上使用下面的代码。largeIcon将作为一个主要的通知图标,您还需要为smallIcon提供一个剪影,因为它将显示在largeIcon的右下角。

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
     builder.setColor(context.getResources().getColor(R.color.red));
     builder.setSmallIcon(R.drawable.some_notification_icon);
     builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
}

在你的构建器中只使用. setsmallicon (r. mimmap .ic_launcher)。

完全同意用户Daniel Saidi的观点。为了有颜色的NotificationIcon,我写这个答案。

为此,你必须创建像剪影这样的图标,并使某些部分透明,无论你想添加颜色的地方。也就是说,

你可以使用

.setColor (your_color_resource_here)

注意:setColor只在Lollipop中可用,所以,你必须检查OSVersion

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    Notification notification = new Notification.Builder(context)
    ...
} else {
    // Lollipop specific setColor method goes here.
    Notification notification = new Notification.Builder(context)
    ...
    notification.setColor(your_color)
    ...            
}

您也可以使用Lollipop作为目标SDK来实现这一点。

关于NotificationIcon的所有说明均在谷歌开发人员控制台通知指南行中给出。

首选通知图标大小24x24dp

mdpi    @ 24.00dp   = 24.00px
hdpi    @ 24.00dp   = 36.00px
xhdpi   @ 24.00dp   = 48.00px

更多信息请参考通知图标大小的链接。

如果你正在使用GoogleFireBaseMessaging,你可以在“通知”有效载荷中设置“图标id”(它帮助我解决了白色栏图标的问题):

{
    "to":"<fb_id>",
    "priority" : "high",
    "notification" : 
    {
        "title" : "title",
        "body" : "body" ,
        "sound" : "default",
        "icon" : "ic_notification"
    }
}

将ic_notification设置为R.drawable中自己的id。