我有一个应用程序显示自定义通知。问题是在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" />
其他回答
完全同意用户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
更多信息请参考通知图标大小的链接。
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上测试。
这是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" />
android:targetSdkVersion="20",它应该< 21。
通知是灰色的,如下所述。不管别人写了什么,它们都不是黑白分明的。你可能见过带有多种色调的图标,比如网络强度条。
在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!)
推荐文章
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?
- 操作栏和新引入的工具栏有什么不同?
- 调整浮动动作按钮图标大小(fab)
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- android:在触摸移动时移动视图
- 如何以编程方式将ID分配给视图?
- 如何解决INSTALL_FAILED_DEXOPT错误?
- 有没有办法自动安装Android SDK ?
- 如何检查一个视图在Android中是否可见?