【问题标题】:Setting a single line android notification (only title)设置单行android通知(仅标题)
【发布时间】:2014-05-10 12:40:33
【问题描述】:

我想在通知抽屉中为我的应用设置一个通知,但其中只有一个标题(没有正文),这意味着 - 只有一行通知,应该与应用图标垂直对齐。

例如,在以下通知中,我只想保留“Ringer shushed 'til 19:16”标题,字体大小相同,但垂直居中于左侧的应用图标。

这是我创建通知的代码:

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);

PendingIntent deleteIntent = PendingIntent.getService(context, 0,
                new Intent(context, GCMIntentService.class)
                        .setAction(IntentConsts.ACTION_CLEAR_MESSAGE_COUNT), PendingIntent.FLAG_CANCEL_CURRENT);

manager.notify(MMConfig.NTF_ID_GCM, new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.push_icon)
                .setContentTitle("My title")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setDeleteIntent(deleteIntent)
                .build());

现在我成功地创建了只有标题的通知,但我似乎无法让它与应用程序图标垂直居中。有什么想法吗?

【问题讨论】:

  • 你见过其他实现这一目标的应用吗?
  • 你的意思是垂直居中吗? Google Now 似乎有单行通知,但它们没有居中。
  • @weston 不,但这似乎是一件经典的事情,我没有看到任何关于它的文档。
  • @RichardLeMesurier 是的,垂直居中。
  • 在这里查看答案stackoverflow.com/a/16168704/383414,了解如何进行自定义布局。

标签: android vertical-alignment android-notifications


【解决方案1】:

我认为这是不可能的。 即使我尝试了一些方法来实现你的想法。但似乎没有什么能按你想要的方式工作。 至于考虑对齐方式,我认为在垂直对齐方式中它实际上也不可能。 即使您尝试删除标题,标题的位置仍然充满空白区域,覆盖了通知区域的一半以上,所以即使这样......通知的正文似乎仍然没有正确居中对齐.

我还尝试对齐文本视图的内容,然后将其用于 setLatestEventInfo() 中的值,但就通知考虑而言,甚至水平对齐似乎都不起作用。

我可能错了,但以我所经历的研究水平来看,这似乎是不可能的。让我知道是否找到任何替代方法:P

【讨论】:

    【解决方案2】:

    像一些建议的那样,我使用RemoteViews 来自定义我的通知,如下所示:

    final RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_new_message);
    
    views.setTextViewText(R.id.title, title);
    
    Notification notification = new Notification();
    notification.contentView = views;
    notification.icon = R.drawable.push_icon;
    notification.contentIntent = contentIntent;
    
    manager.notify(Config.NTF_ID_GCM, notification);
    

    我在notification_new_message.xml 中设置通知的理想外观。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多