【问题标题】:How to highlight left hand side text of every line in notification如何突出显示通知中每一行的左侧文本
【发布时间】:2014-07-18 00:02:36
【问题描述】:

目前,我已经实现了多行通知

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this.getApplicationContext())
            .setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(contentTitle)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setTicker(ticker)
            .setContentText(contentText);

    // Need BIG view?
    if (size > 1) {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle(contentTitle);
        for (String notificationMessage : notificationMessages) {
            inboxStyle.addLine(notificationMessage);
        }
        mBuilder.setStyle(inboxStyle);
    }

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

然而,这并不是我想要的。当我查看 GMail 实现时,我意识到每一行的左侧文本 (Yan Cheng Cheok) 都被突出显示。

我想知道,达到这种效果的技术是什么?因为我想突出显示我的股票名称。

【问题讨论】:

    标签: android


    【解决方案1】:

    代码inboxStyle.addLine(notificationMessage); 接受CharSequence 作为参数,而不是String

    所以你可以通过申请UnderlineSpan轻松达到你想要的效果

    遵循参考链接:

    https://developer.android.com/reference/android/text/style/UnderlineSpan.html

    How to set underline text on textview?

    快速编辑:

    我只是意识到OP提到highlight而不是underline但答案还是一样的,有几种类型的CharacterSpan OP可以选择自定义文本:

    https://developer.android.com/reference/android/text/style/CharacterStyle.html

    【讨论】:

      【解决方案2】:

      您可以为通知使用自定义视图。有关更多信息和一些示例,请参阅这些 SO 问题:

      Change Notification Layout

      Create custom notification, android

      这应该会让你朝着正确的方向前进。

      【讨论】:

        【解决方案3】:

        您有几个选项可以使某些文本变为粗体。

        第一个选项是使用Html.fromHtml(String) 并在您的文本中添加一个粗体标记,如下所示:

        for (String notificationMessage : notificationMessages) {
            CharSequence content = Html.fromHTML("<b>This is bold</b> and this is not");
            inboxStyle.addLine(content);
        }
        

        如果你不想使用 HTML,你也可以使用 Span 来完成同样的事情:

        Spannable sb = new SpannableString("Bold text");
        sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length() - 1, 
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        inboxStyle.addLine(sb);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-31
          相关资源
          最近更新 更多