Android 5 Lollipop 通知栏图标变白
Notification bar icon turns white in Android 5 Lollipop
我有一个显示自定义通知的应用程序。问题是当 运行 in Android 5 通知栏中的小图标显示为白色。我该如何解决这个问题?
这是 Android 用于显示通知图标的代码:
// android_frameworks_base/packages/SystemUI/src/com/android/systemui/
// statusbar/BaseStatusBar.java
if (entry.targetSdk >= Build.VERSION_CODES.LOLLIPOP) {
entry.icon.setColorFilter(mContext.getResources().getColor(android.R.color.white));
} else {
entry.icon.setColorFilter(null);
}
因此您需要将目标 sdk 版本设置为 <21
并且图标将保持彩色。这是一个丑陋的解决方法,但它做了预期的事情。 无论如何,我真的建议关注Google的Design Guidelines: "Notification icons must be entirely white."
以下是实现方法:
如果您使用 Gradle/Android Studio 构建您的应用程序,请使用 build.gradle
:
defaultConfig {
targetSdkVersion 20
}
否则(Eclipse 等)使用 AndroidManifest.xml
:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="20" />
android:targetSdkVersion="20"
应该是 < 21
.
接受的答案不(完全)正确。当然,它使通知图标显示为彩色,但这样做有一个很大的缺点 - 通过将目标 SDK 设置为低于 Android Lollipop!
如果您按照建议通过将目标 SDK 设置为 20 来解决白色图标问题,您的应用将不会以 Android Lollipop 为目标,这意味着您无法使用 Lollipop 特定的功能。
看看 http://developer.android.com/design/style/iconography.html,您会发现白色样式是通知在 Android Lollipop 中的显示方式。
在 Lollipop 中,Google 还建议您使用将显示在(白色)通知图标后面的颜色 - 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 的观点。为了 Color
对应 NotificationIcon
我写了这个答案。
为此,您必须制作像 Silhouette
这样的图标,并在您想添加 Colors
的任何地方制作一些部分 Transparent
。即,
您可以使用
添加颜色
.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
的所有说明在 Google Developer Console Notification Guide Lines 给出。
首选通知图标大小 24x24dp
mdpi @ 24.00dp = 24.00px
hdpi @ 24.00dp = 36.00px
xhdpi @ 24.00dp = 48.00px
有关 Notification Icon Sizes 的更多信息,请参阅此 link。
我遇到了同样的问题,这是因为我的应用通知图标不是扁平的。对于 android 版本的 lollipop 甚至低于 lollipop,您的应用通知图标应该是扁平的,不要使用带有阴影等的图标。
下面是在所有 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());
}
错误的图标
右侧图标
从 manifest.xml 中删除 android:targetSdkVersion="21"
。它会工作!
从这个开始,你的 apk 中根本没有任何问题,这只是我应用它的一个技巧,我在通知中发现了彩色图标
为避免通知图标变白,请为它们使用 "Silhouette" 图标,即。白色透明背景图像。
您可以使用 Irfanview 来构建它们:
- 选择一张图片,在
IrfanView
中打开,按F12选择绘画工具,必要时清理图片(去除不需要的部分,平滑和润色)
Image / Decrease Color Depth
到 2(对于黑白图片)
Image / Negative
(对于黑底白字图片)
Image / Resize/Resample
到 144 x 144(使用大小方法 "Resize" 而不是 "Resample",否则图片再次增加到每像素 24 色位 (24 BPP)
File / Save as PNG
,勾选Show option dialog
,勾选Save Transparent Color
,点击Save
,然后点击图片中的黑色设置透明色
Android 似乎仅使用 drawable-xxhdpi 图片分辨率 (144 x 144),因此将生成的 ic_notification.png
文件复制到 \AndroidStudio\Projects\...\app\src\main\res\drawable-xxhdpi
。在您的代码中使用 .setSmallIcon(R.drawable.ic_notification)
,或按照 Daniel Saidi 上面的建议使用 getNotificationIcon()
。
alpha-channel 是 Android 用于通知图标的唯一图像数据:
alpha == 1
:像素显示白色
alpha == 0
:像素显示为您在 Notification.Builder#setColor(int)
处选择的颜色
这在 https://developer.android.com/about/versions/android-5.0-changes.html 中提到:
The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only.
几乎所有 built-in drawables 似乎都适合此 alpha 图像,因此您可以使用类似的东西:
Notification.Builder.setColor(Color.RED)
.setSmallIcon(android.R.drawable.star_on)
但我仍在寻找正式证实这一点的API doc。
测试于 Android 22.
另一种选择是利用版本特定的可绘制对象 (mipmap) 目录为 Lollipop 及更高版本提供不同的图形。
在我的应用程序中,"v21" 目录包含带有透明文本的图标,而其他目录包含非透明版本(对于早于 Lollipop 的 Android 版本)。
应该看起来像这样:
这样,您就不需要检查代码中的版本号,例如
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
同样,如果您使用 "icon" 属性,您可以在 GCM 负载中引用 "ic_notification"(或您选择的任何名称)。
https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support
Post android Lollipop 版本 android 更改了在通知栏中显示通知图标的准则。
官方文档说“更新或删除涉及颜色的资产。系统会忽略操作图标和主通知图标中的所有非 alpha 通道。您应该假设这些图标将仅是 alpha。系统将通知图标绘制为白色和深灰色的动作图标。”
现在,外行人的意思是 "Convert all parts of the image that you don’t want to show to transparent pixels. All colors and non transparent pixels are displayed in white"
您可以在此处通过屏幕截图详细了解如何执行此操作
https://blog.clevertap.com/fixing-notification-icon-for-android-lollipop-and-above/
希望对您有所帮助
如果您使用的是 GoogleFireBaseMessaging,则可以在 "notification" 有效载荷中设置 "icon id"(它帮助我解决了白条图标问题):
{
"to":"<fb_id>",
"priority" : "high",
"notification" :
{
"title" : "title",
"body" : "body" ,
"sound" : "default",
"icon" : "ic_notification"
}
}
将 ic_notification 设置为您自己来自 R.drawable 的 ID。
根据 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.mipmap.ic_launcher)
与您的构建器一起使用。
现在android工作室提供了一个插件Image Asset,它将在所有需要的drawbale文件夹中生成图标
Image Asset Studio 可帮助您创建不同密度的各种类型的图标,并准确显示它们在项目中的放置位置。它包括用于调整图标和添加背景的工具,同时在预览窗格中显示结果,因此它们完全按照您的预期显示。这些工具可以极大地简化图标设计和导入过程。
您可以访问 Image Asset,方法是单击新建 > 单击 Image Asset 选项,它将显示 window,如下所示:-
通知是灰度,如下所述。他们不是 black-and-white,不管其他人写了什么。您可能见过具有多种阴影的图标,例如网络强度条。
在 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
:
致电NotificationCompat.Builder.setColor(int argb)
。来自 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.
我对 setColor 的测试显示 Alpha 组件未被忽略;相反,他们仍然提供灰度。较高的 Alpha 值将像素变为白色。较低的 Alpha 值会将通知区域中的像素变为背景色(在我的设备上为黑色),或变为 pull-down 通知中的指定颜色。 (似乎其他人报告的行为略有不同,所以请注意!)
我在这方面也遇到了太多问题,但在网上搜索后,我找到了不同的解决方案issue.Let我总结了所有的解决方案并解释:
注意:此解决方案适用于 Phonegap cordova 用户
- Example
<preference name="android-targetSdkVersion" value="20"/>
您需要将 android-targetSdkVersion 值设置为小于 21。
所以在设置这个值后,通知图标图像将出现到 Android 6(Marshmallow),它不会在 Android 7(Nougat) 中工作。
这个解决方案对我有用。
- 更改配置文件中的 statusbarStyle。
Example
<preference name="StatusBarStyle" value="lightcontent" />
但此解决方案仅在您的应用程序打开时有效。
所以,我想这个解决方案不是最好的解决方案,但它适用于许多用户。
- 使您的图标透明。
这个解决方案对很多人都有效。
实际上,在开发 Native 应用程序时,我们需要为他们提供三个图像:
(a)应用程序图标
(b)通知图标
(c) 状态栏图标图像,但在混合移动应用程序开发的情况下,没有这样做的选项。
所以让你的图标透明,这个解决方案将解决你的问题。
而且我相信上述解决方案之一可以解决您的问题。
仅供参考:如果图标未出现,请确保您的本地或远程通知配置包含正确的图标名称,即
'largeIcon' => 'ic_launcher',
'smallIcon' => 'ic_launcher' // defaults to ic_launcher,
在应用中混合这两个东西gradle
defaultConfig {
applicationId "com.example.abdulwahidvi.notificationproblem"
minSdkVersion 16
//This is the version that was added by project by default
targetSdkVersion 26 <------ default
// Changed it to version 20
targetSdkVersion 20 <------ mine
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
我认为谈论API 21 已经太晚了,但我找到了一个简单的方法。
使用“自定义通知(自定义布局)”时,
RemoteView 的
setImageViewResource(int viewId, int srcId);
和
setImageViewUri(int viewId, Uri uri);
使 Lollipop 上的图像变白 (API 21)。
但是当使用
setImageViewBitmap(int viewId, Bitmap bitmap);
图片没有白化!
您需要导入单色透明PNG图片。所以你可以设置小图标的图标颜色。否则在某些设备如MOTO
会显示为白色
根据文档,通知图标必须为白色,因为 Android 3.0(API 级别 11) :
https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar
"Status bar icons are composed simply of white pixels on a transparent
backdrop, with alpha blending used for smooth edges and internal
texture where appropriate."
我有一个显示自定义通知的应用程序。问题是当 运行 in Android 5 通知栏中的小图标显示为白色。我该如何解决这个问题?
这是 Android 用于显示通知图标的代码:
// android_frameworks_base/packages/SystemUI/src/com/android/systemui/
// statusbar/BaseStatusBar.java
if (entry.targetSdk >= Build.VERSION_CODES.LOLLIPOP) {
entry.icon.setColorFilter(mContext.getResources().getColor(android.R.color.white));
} else {
entry.icon.setColorFilter(null);
}
因此您需要将目标 sdk 版本设置为 <21
并且图标将保持彩色。这是一个丑陋的解决方法,但它做了预期的事情。 无论如何,我真的建议关注Google的Design Guidelines: "Notification icons must be entirely white."
以下是实现方法:
如果您使用 Gradle/Android Studio 构建您的应用程序,请使用 build.gradle
:
defaultConfig {
targetSdkVersion 20
}
否则(Eclipse 等)使用 AndroidManifest.xml
:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="20" />
android:targetSdkVersion="20"
应该是 < 21
.
接受的答案不(完全)正确。当然,它使通知图标显示为彩色,但这样做有一个很大的缺点 - 通过将目标 SDK 设置为低于 Android Lollipop!
如果您按照建议通过将目标 SDK 设置为 20 来解决白色图标问题,您的应用将不会以 Android Lollipop 为目标,这意味着您无法使用 Lollipop 特定的功能。
看看 http://developer.android.com/design/style/iconography.html,您会发现白色样式是通知在 Android Lollipop 中的显示方式。
在 Lollipop 中,Google 还建议您使用将显示在(白色)通知图标后面的颜色 - 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 的观点。为了 Color
对应 NotificationIcon
我写了这个答案。
为此,您必须制作像 Silhouette
这样的图标,并在您想添加 Colors
的任何地方制作一些部分 Transparent
。即,
您可以使用
添加颜色.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
的所有说明在 Google Developer Console Notification Guide Lines 给出。
首选通知图标大小 24x24dp
mdpi @ 24.00dp = 24.00px
hdpi @ 24.00dp = 36.00px
xhdpi @ 24.00dp = 48.00px
有关 Notification Icon Sizes 的更多信息,请参阅此 link。
我遇到了同样的问题,这是因为我的应用通知图标不是扁平的。对于 android 版本的 lollipop 甚至低于 lollipop,您的应用通知图标应该是扁平的,不要使用带有阴影等的图标。
下面是在所有 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());
}
错误的图标
右侧图标
从 manifest.xml 中删除 android:targetSdkVersion="21"
。它会工作!
从这个开始,你的 apk 中根本没有任何问题,这只是我应用它的一个技巧,我在通知中发现了彩色图标
为避免通知图标变白,请为它们使用 "Silhouette" 图标,即。白色透明背景图像。 您可以使用 Irfanview 来构建它们:
- 选择一张图片,在
IrfanView
中打开,按F12选择绘画工具,必要时清理图片(去除不需要的部分,平滑和润色) Image / Decrease Color Depth
到 2(对于黑白图片)Image / Negative
(对于黑底白字图片)Image / Resize/Resample
到 144 x 144(使用大小方法 "Resize" 而不是 "Resample",否则图片再次增加到每像素 24 色位 (24 BPP)File / Save as PNG
,勾选Show option dialog
,勾选Save Transparent Color
,点击Save
,然后点击图片中的黑色设置透明色
Android 似乎仅使用 drawable-xxhdpi 图片分辨率 (144 x 144),因此将生成的 ic_notification.png
文件复制到 \AndroidStudio\Projects\...\app\src\main\res\drawable-xxhdpi
。在您的代码中使用 .setSmallIcon(R.drawable.ic_notification)
,或按照 Daniel Saidi 上面的建议使用 getNotificationIcon()
。
alpha-channel 是 Android 用于通知图标的唯一图像数据:
alpha == 1
:像素显示白色alpha == 0
:像素显示为您在Notification.Builder#setColor(int)
处选择的颜色
这在 https://developer.android.com/about/versions/android-5.0-changes.html 中提到:
The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only.
几乎所有 built-in drawables 似乎都适合此 alpha 图像,因此您可以使用类似的东西:
Notification.Builder.setColor(Color.RED)
.setSmallIcon(android.R.drawable.star_on)
但我仍在寻找正式证实这一点的API doc。
测试于 Android 22.
另一种选择是利用版本特定的可绘制对象 (mipmap) 目录为 Lollipop 及更高版本提供不同的图形。
在我的应用程序中,"v21" 目录包含带有透明文本的图标,而其他目录包含非透明版本(对于早于 Lollipop 的 Android 版本)。
应该看起来像这样:
这样,您就不需要检查代码中的版本号,例如
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
同样,如果您使用 "icon" 属性,您可以在 GCM 负载中引用 "ic_notification"(或您选择的任何名称)。
https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support
Post android Lollipop 版本 android 更改了在通知栏中显示通知图标的准则。 官方文档说“更新或删除涉及颜色的资产。系统会忽略操作图标和主通知图标中的所有非 alpha 通道。您应该假设这些图标将仅是 alpha。系统将通知图标绘制为白色和深灰色的动作图标。” 现在,外行人的意思是 "Convert all parts of the image that you don’t want to show to transparent pixels. All colors and non transparent pixels are displayed in white"
您可以在此处通过屏幕截图详细了解如何执行此操作 https://blog.clevertap.com/fixing-notification-icon-for-android-lollipop-and-above/
希望对您有所帮助
如果您使用的是 GoogleFireBaseMessaging,则可以在 "notification" 有效载荷中设置 "icon id"(它帮助我解决了白条图标问题):
{
"to":"<fb_id>",
"priority" : "high",
"notification" :
{
"title" : "title",
"body" : "body" ,
"sound" : "default",
"icon" : "ic_notification"
}
}
将 ic_notification 设置为您自己来自 R.drawable 的 ID。
根据 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.mipmap.ic_launcher)
与您的构建器一起使用。
现在android工作室提供了一个插件Image Asset,它将在所有需要的drawbale文件夹中生成图标
Image Asset Studio 可帮助您创建不同密度的各种类型的图标,并准确显示它们在项目中的放置位置。它包括用于调整图标和添加背景的工具,同时在预览窗格中显示结果,因此它们完全按照您的预期显示。这些工具可以极大地简化图标设计和导入过程。
您可以访问 Image Asset,方法是单击新建 > 单击 Image Asset 选项,它将显示 window,如下所示:-
通知是灰度,如下所述。他们不是 black-and-white,不管其他人写了什么。您可能见过具有多种阴影的图标,例如网络强度条。
在 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
:
致电
NotificationCompat.Builder.setColor(int argb)
。来自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.
我对 setColor 的测试显示 Alpha 组件未被忽略;相反,他们仍然提供灰度。较高的 Alpha 值将像素变为白色。较低的 Alpha 值会将通知区域中的像素变为背景色(在我的设备上为黑色),或变为 pull-down 通知中的指定颜色。 (似乎其他人报告的行为略有不同,所以请注意!)
我在这方面也遇到了太多问题,但在网上搜索后,我找到了不同的解决方案issue.Let我总结了所有的解决方案并解释:
注意:此解决方案适用于 Phonegap cordova 用户
- Example
<preference name="android-targetSdkVersion" value="20"/>
您需要将 android-targetSdkVersion 值设置为小于 21。 所以在设置这个值后,通知图标图像将出现到 Android 6(Marshmallow),它不会在 Android 7(Nougat) 中工作。 这个解决方案对我有用。
- 更改配置文件中的 statusbarStyle。 Example
<preference name="StatusBarStyle" value="lightcontent" />
但此解决方案仅在您的应用程序打开时有效。 所以,我想这个解决方案不是最好的解决方案,但它适用于许多用户。
- 使您的图标透明。 这个解决方案对很多人都有效。 实际上,在开发 Native 应用程序时,我们需要为他们提供三个图像: (a)应用程序图标 (b)通知图标 (c) 状态栏图标图像,但在混合移动应用程序开发的情况下,没有这样做的选项。 所以让你的图标透明,这个解决方案将解决你的问题。
而且我相信上述解决方案之一可以解决您的问题。
仅供参考:如果图标未出现,请确保您的本地或远程通知配置包含正确的图标名称,即
'largeIcon' => 'ic_launcher',
'smallIcon' => 'ic_launcher' // defaults to ic_launcher,
在应用中混合这两个东西gradle
defaultConfig {
applicationId "com.example.abdulwahidvi.notificationproblem"
minSdkVersion 16
//This is the version that was added by project by default
targetSdkVersion 26 <------ default
// Changed it to version 20
targetSdkVersion 20 <------ mine
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
我认为谈论API 21 已经太晚了,但我找到了一个简单的方法。
使用“自定义通知(自定义布局)”时,
RemoteView 的
setImageViewResource(int viewId, int srcId);
和
setImageViewUri(int viewId, Uri uri);
使 Lollipop 上的图像变白 (API 21)。
但是当使用
setImageViewBitmap(int viewId, Bitmap bitmap);
图片没有白化!
您需要导入单色透明PNG图片。所以你可以设置小图标的图标颜色。否则在某些设备如MOTO
会显示为白色根据文档,通知图标必须为白色,因为 Android 3.0(API 级别 11) :
https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar
"Status bar icons are composed simply of white pixels on a transparent backdrop, with alpha blending used for smooth edges and internal texture where appropriate."