如何在Android的通知区域和通知抽屉中使用其他通知图标 [英] How to use different notification icon in Android for the notification area and the notification drawer

查看:59
本文介绍了如何在Android的通知区域和通知抽屉中使用其他通知图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过在透明的白色背景中使用.png来设置图标.在锁定屏幕和通知区域中显示时,一切正常.

I'm setting an icon, by using a .png in white, transparent background. It's working ok when displaying in the lock screen, and in the notification area.

该图标以黄色突出显示:在此处输入图片描述

The icon is the highlighted in yellow: enter image description here

现在在通知抽屉中,图标也显示为白色(是相同的图标).在此处输入图片描述

Now in the notification drawer the icon is shown in white too (is the same icon). enter image description here

但是我想使用另一个图标,在这种情况下,卡车为蓝色.现在,我通过使用Notification.Builder.SetColor()将颜色设置为蓝色,标题以该颜色显示.

But I would like to use another icon, in this case the truck in blue. Right now I'm setting the color to blue by using Notification.Builder.SetColor(), and the title appears in that color.

您可以在状态栏中看到Gmail图标以白色显示,在抽屉中以红色显示.

You can see how the Gmail icon displays in white in the status bar, and red in the drawer.

这是构建通知的实际代码.代码适用于Xamarin.Droid

This is the actual code to build the notification. Code is for Xamarin.Droid

var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

Notification.Builder builder = new Notification.Builder(this)
                .SetContentTitle(title)
                .SetContentText(desc)
                .SetSmallIcon(Routes.Droid.Resource.Drawable.truck)
                .SetAutoCancel(true);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.truckColor));
builder.SetColor(Android.Graphics.Color.Rgb(33,150,243));

var notification = new Notification.BigTextStyle(builder).BigText(desc).Build();

notificationManager.Notify(-1, notification);

如何设置通知抽屉中的图标颜色,或使用其他图标?

How can I set the icon color in the notification drawer, or use different icons?

目标是在通知区域中以白色显示图标,在通知抽屉中以蓝色显示图标.

The objective is to display the icon in white in the notification area, and in blue in the notification drawer.

推荐答案

图标颜色由Android 7.0(API 24)自动设置.

The icon color is set automatically by Android 7.0 (API 24).

对于要由Android着色的图标,您必须使用用于不同密度的特定资源,然后将它们添加到相应的目录中.

For the icon to be colored by Android, you have to use specific resources for the different densities, and add them in the corresponding directories.

在我的应用中,我将图标添加到了

In my app, I added the icons to

/Resources/drawable-hdpi

/Resources/drawable-hdpi

/Resources/drawable-xhdpi

/Resources/drawable-xhdpi

/Resources/drawable-xxhdpi

/Resources/drawable-xxhdpi

如果像我这样,有一个图标图像,则可以使用以下工具来创建新资源:

If you, like in my case, have an image for the icon, you can create the new resources by using this tool: https://romannurik.github.io/AndroidAssetStudio/icons-notification.html

源代码不会改变,但是当设置不同密度的图标时,图标在抽屉中为彩色,在通知区域中为白色.

Source code doesn't change, but when setting the icons for the diferent densities, the icon get's colored in the drawer, and white in the notification area.

以前,我只是在Resources/drawable文件夹中使用默认的.png图标.这样会显示图标,但始终以相同的颜色显示.

Previously I was just using a default .png icon in the Resources/drawable folder. This displays the icon, but it appears always in the same color.

这篇关于如何在Android的通知区域和通知抽屉中使用其他通知图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆