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


当前回答

公认的答案并不(完全)正确。当然,它使通知图标显示颜色,但这样做有一个很大的缺点-通过设置目标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;
}

其他回答

完全同意用户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

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

alpha-channel是Android用于通知图标的图像的唯一数据:

Alpha == 1:像素显示为白色 alpha == 0:像素显示为您选择的Notification.Builder#setColor(int)

https://developer.android.com/about/versions/android-5.0-changes.html上提到了这一点:

系统会忽略操作图标和主通知图标中的所有非alpha通道。您应该假设这些图标都是纯alpha的。

几乎所有的内置绘图似乎都适合alpha图像,所以你可能会使用类似的东西:

Notification.Builder.setColor(Color.RED)
                    .setSmallIcon(android.R.drawable.star_on)

但我仍在寻找官方证实这一点的API文档。

在Android 22上测试。

我认为现在谈论API 21已经太晚了,但我找到了一个简单的方法。

使用“自定义通知(自定义布局)”时,

RemoteView的

setImageViewResource(int viewId, int srcId);

and

setImageViewUri(int viewId, Uri uri); 

使这些图像白色的棒棒糖(API 21)。

但是当使用

setImageViewBitmap(int viewId, Bitmap bitmap);

图像不会变成白色面具!

我也遇到过同样的问题,那是因为我的应用通知图标不平坦。对于android版本,你的应用通知图标应该是扁平的,不要使用带有阴影的图标。

下面的代码在所有android版本上都运行得很好。

private void sendNotification(String msg) {

    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, CheckOutActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).setLights(Color.GREEN, 300, 300)
            .setVibrate(new long[] { 100, 250 })
            .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(new Random().nextInt(), mBuilder.build());
}

错误的图标

正确的图标

您需要导入单色透明PNG图像。所以你可以设置小图标的图标颜色。否则,它将显示白色在一些设备,如MOTO