【问题标题】:how to display proper small icon for notification is not getting displayed in Lollipop and Android M如何在 Lollipop 和 Android M 中显示正确的通知小图标
【发布时间】:2015-08-28 17:28:27
【问题描述】:

我正在创建一个应用程序,我能够正确显示通知,但没有显示小图标,正如我在可绘制文件夹中提到的那样,该图标被白色掩盖。任何人都可以帮助我,我怎样才能让图标正常显示。

以下是我的通知代码:

nb = new NotificationCompat.Builder(context)
        .setContentTitle(contentTitle)
        .setContentText(contentText)
        .setSmallIcon(icon)
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.micon_notification))
        .setWhen(when)
        .setContentIntent(contentIntent)
        .setAutoCancel(true)
        .setOnlyAlertOnce(true)
        .setTicker(tickerText)
        .setColor(Color.RED);

drawable中提到的图标如下图:

[1]: http://i.stack.imgur.com/ggYCY.png

图像中出现的完全红色消失了,图标显示为完全白色。欢迎所有建议。

【问题讨论】:

    标签: android android-5.0-lollipop android-notifications


    【解决方案1】:

    这是 Android 用来显示通知图标的代码:

    if (entry.targetSdk >= Build.VERSION_CODES.LOLLIPOP) {
        entry.icon.setColorFilter(mContext.getResources().getColor(android.R.color.white));
    } else {
        entry.icon.setColorFilter(null);
    }
    

    为此,您必须制作像剪影这样的图标,并在要添加颜色的任何地方使某些部分透明。

    您可以使用添加颜色

    .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)
        ...            
    }
    

    查看文档:http://developer.android.com/design/style/iconography.html

    有话:

    "通知图标必须全白。另外,系统可能会缩放 向下和/或变暗图标。”

    希望对你有帮助!

    【讨论】:

    • 我使用了set color,它为图标的背景添加颜色,不适用于setcolor方法。
    • icon.setColorFilter中的entry是什么??
    • 是的 setColor 用于更改背景颜色。你必须为棒棒糖设计不同的图标,如上图所示。在棒棒糖之前的版本中,您可以使用现有图标。您也可以关注图标链接。
    • 这只是 android 源代码向您解释,android 本身将过滤器设置为棒棒糖的白色,这就是为什么您现有的图标在棒棒糖中显示为白色
    • 这里是链接:developer.android.com/about/versions/android-5.0-changes.html 关于 android lollipop 通知更改
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    相关资源
    最近更新 更多