我有一个应用程序显示自定义通知。问题是在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,图标将保持颜色。这是一个丑陋的解决方案,但它做到了预期的效果。无论如何,我真的建议遵循谷歌的设计准则:“通知图标必须完全是白色的。”
下面是你如何实现它:
如果你使用Gradle/Android Studio来构建应用,请使用build. Gradle:
defaultConfig {
targetSdkVersion 20
}
否则(Eclipse等)使用AndroidManifest.xml:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="20" />
公认的答案并不(完全)正确。当然,它使通知图标显示颜色,但这样做有一个很大的缺点-通过设置目标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
更多信息请参考通知图标大小的链接。
我也遇到过同样的问题,那是因为我的应用通知图标不平坦。对于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());
}
错误的图标
正确的图标
从manifest.xml中删除android:targetSdkVersion="21"它会起作用的! 从这一点上看,在你的apk中没有任何问题,这只是一个技巧,我应用这个,我在通知中发现了彩色图标
为了避免通知图标变成白色,使用“剪影”图标。透明背景图片。 你可以使用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。
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上测试。
另一种选择是利用特定于版本的可绘制(mipmap)目录为Lollipop和以上版本提供不同的图形。
在我的应用程序中,“v21”目录包含带有透明文本的图标,而其他目录包含非透明版本(适用于比棒棒糖更老的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 release android has changed the guidelines for displaying notification icons in the Notification bar. The official documentation says "Update or remove assets that involve color. 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. The system draws notification icons in white and action icons in dark gray.” Now what that means in lay man terms is "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,你可以在“通知”有效载荷中设置“图标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. mimmap .ic_launcher)。
现在android studio提供了一个插件图像资产,它将在所有需要的drawbale文件夹中生成图标
Image Asset Studio可以帮助您在不同密度下创建各种类型的图标,并向您显示它们将被放置在项目中的确切位置。它包括调整图标和添加背景的工具,同时在预览窗格中显示结果,因此它们完全按照您的预期显示。这些工具可以极大地简化图标设计和导入过程。
你可以通过点击新建>来访问图像资产,点击图像资产选项,它将显示如下窗口
通知是灰色的,如下所述。不管别人写了什么,它们都不是黑白分明的。你可能见过带有多种色调的图标,比如网络强度条。
在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!)
我也遇到了太多的问题,但在互联网上搜索后,我找到了这个问题的不同解决方案。让我总结一下所有的解决方案并解释一下:
注意:此解决方案适用于Phonegap cordova用户
例子
<预览
您需要将android-targetSdkVersion值设置为小于21。 所以在设置这个值后,通知图标图像将出现到Android 6(棉花糖),它不会在Android 7(牛轧糖)工作。 这个解决方案对我很有效。
更改配置文件中的statusbarStyle。 例子
<预览
但这个解决方案只有在应用程序打开时才有效。 所以,我想这个解决方案不是最好的解决方案,但它适用于许多用户。
让你的图标透明。 这个方法对很多人都有效。 实际上,在本地应用程序的开发中,我们需要为他们提供三个图像: (一)应用程序图标 (b)通知图标 (c)状态栏图标图像,但在混合移动应用程序开发的情况下,没有这样做的选项。 所以让你的图标透明,这个解决方案将解决你的问题。
我相信上述解决方案之一将为您的问题工作。
供您参考:如果图标没有出现,请确保您的本地或远程通知配置包含正确的图标名称
'largeIcon' => 'ic_launcher',
'smallIcon' => 'ic_launcher' // defaults to ic_launcher,
在app 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);
and
setImageViewUri(int viewId, Uri uri);
使这些图像白色的棒棒糖(API 21)。
但是当使用
setImageViewBitmap(int viewId, Bitmap bitmap);
图像不会变成白色面具!
根据文档,从Android 3.0 (API Level 11)开始,通知图标必须是白色的:
https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar
“状态栏图标是由透明屏幕上的白色像素组成的 背景,使用alpha混合用于光滑的边缘和内部 适当的纹理。”
推荐文章
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态