我有一个应用程序显示自定义通知。问题是在Android 5中运行时,通知栏中的小图标显示为白色。我该如何解决这个问题?
当前回答
根据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设计指南,你必须为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)。
为了避免通知图标变成白色,使用“剪影”图标。透明背景图片。 你可以使用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;
}
另一种选择是利用特定于版本的可绘制(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
我也遇到了太多的问题,但在互联网上搜索后,我找到了这个问题的不同解决方案。让我总结一下所有的解决方案并解释一下:
注意:此解决方案适用于Phonegap cordova用户
例子
<预览
您需要将android-targetSdkVersion值设置为小于21。 所以在设置这个值后,通知图标图像将出现到Android 6(棉花糖),它不会在Android 7(牛轧糖)工作。 这个解决方案对我很有效。
更改配置文件中的statusbarStyle。 例子
<预览
但这个解决方案只有在应用程序打开时才有效。 所以,我想这个解决方案不是最好的解决方案,但它适用于许多用户。
让你的图标透明。 这个方法对很多人都有效。 实际上,在本地应用程序的开发中,我们需要为他们提供三个图像: (一)应用程序图标 (b)通知图标 (c)状态栏图标图像,但在混合移动应用程序开发的情况下,没有这样做的选项。 所以让你的图标透明,这个解决方案将解决你的问题。
我相信上述解决方案之一将为您的问题工作。
推荐文章
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?
- 操作栏和新引入的工具栏有什么不同?
- 调整浮动动作按钮图标大小(fab)
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- android:在触摸移动时移动视图
- 如何以编程方式将ID分配给视图?
- 如何解决INSTALL_FAILED_DEXOPT错误?
- 有没有办法自动安装Android SDK ?
- 如何检查一个视图在Android中是否可见?
- Android是否保留。apk文件?如果有,在哪里?
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?