我有一个应用程序显示自定义通知。问题是在Android 5中运行时,通知栏中的小图标显示为白色。我该如何解决这个问题?
当前回答
根据文档,从Android 3.0 (API Level 11)开始,通知图标必须是白色的:
https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar
“状态栏图标是由透明屏幕上的白色像素组成的 背景,使用alpha混合用于光滑的边缘和内部 适当的纹理。”
其他回答
您需要导入单色透明PNG图像。所以你可以设置小图标的图标颜色。否则,它将显示白色在一些设备,如MOTO
通知是灰色的,如下所述。不管别人写了什么,它们都不是黑白分明的。你可能见过带有多种色调的图标,比如网络强度条。
在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!)
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/
希望这能有所帮助
完全同意用户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
更多信息请参考通知图标大小的链接。
另一种选择是利用特定于版本的可绘制(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
推荐文章
- 如何设置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 -有高度匹配宽度?