【问题标题】:NotificationCompat android - how to show only Large icon without smallNotificationCompat android - 如何只显示大图标而不显示小图标
【发布时间】:2015-06-17 09:08:20
【问题描述】:

当我添加通知时:

        NotificationCompat.Builder mBuilder =
                            new NotificationCompat.Builder(this)  
              .setSmallIcon(R.drawable.plus)
.setContentTitle(title)
.setAutoCancel(true) 
.setContentText(text)
.setSound(RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(bm);

我看到大图标和小图标:

我怎样才能设置只有大图标,没有小。 如果只使用 setLargeIcon,我根本看不到通知,只是发出警报。

【问题讨论】:

  • 我想要这种类型的通知,比如“E”图像,而不是 E 图像角落的圆圈,请帮助我无法获得。

标签: android notifications


【解决方案1】:

小图标是必须的。 如果您不设置大图标,您的小图标会在圆圈中间变大,并带有您选择的颜色 (setColor)。

如果我是你,我会将空白 E 放在 smallicon 的透明背景上,并为圆圈设置红色。

【讨论】:

    【解决方案2】:

    获取小图标id,然后尝试隐藏它

    int smallIconId = ctx.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
    if (smallIconId != 0) { 
        if (notification.contentView!=null)
            notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
    }
    

    试试看这个post 它也会有所帮助

    我在 api 18,23 (samsung j1,galaxy S6) 上测试代码工作正常

    【讨论】:

    • 注意:小图标是必需的,这意味着您应该为此设置值,但是您可以尝试上面的代码来隐藏小图标,...
    • 或者只是设置一个透明的,不要对图标 id 不改变名称做出疯狂的假设
    • 我没有尝试透明小图标,但使用 id 对我有好处;)但如果你这么说,最后这是你的方式
    • 设置透明图标不起作用,因为您在状态栏中看不到您的图标。相反,您应该创建一个自定义内容视图并使用 setContentView 将其传递给构建器
    • 以上代码有助于在用户拉动通知时隐藏小图标。但是在 lint 中显示“notification.contentView”已被弃用,有什么替代方法可以消除警告吗?
    【解决方案3】:

    根据上一个答案,您也可以隐藏已扩展的视图:

    int smallIconId = AnghamiApp.getContext().getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
                if (smallIconId != 0) {
                    notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
                    notification.bigContentView.setViewVisibility(smallIconId, View.INVISIBLE);
                }
    

    【讨论】:

      【解决方案4】:

      您可以创建自定义通知,然后在大通知区域中显示您想要的任何内容。 在此处查看示例TutorialsFace: Build all types of Notifications in Android

      【讨论】:

        【解决方案5】:
        public class MainActivity extends AppCompatActivity {
          @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("Test")
                    .setContentText("Hii There")
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.smallicon))
                    .setAutoCancel(true)
                    .build();
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.notify(123, notification);
          }
        }
        

        【讨论】:

        • 大家好,这是我的第一个答案,对不起,我给出的答案迟了..请尝试这个答案,你会得到标题消息和大图标忽略类的东西,你可以在任何类中实现它
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        • 2021-04-06
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        相关资源
        最近更新 更多