样式通知 InboxStyle [英] Styling notification InboxStyle

查看:8
本文介绍了样式通知 InboxStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现可扩展的通知,并且我使用了 InboxStyle .

I try to implement an extendable notification and I have used the InboxStyle for that.

基于以下来自文档的图片:

应该可以设置文本的样式.在这种情况下,将Google Play"设为粗体.

it should be possible to style the text. In this case make "Google Play" bold.

InboxStyle 只有 addLine() 可以在其中传递 CharSequence.我尝试使用 Html.fromHtml() 并使用了一些 html 格式,但我无法成功.

InboxStyle has only addLine() where I can pass a CharSequence. I tried with Html.fromHtml() and used some html formatting but I couldn't succeed.

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("title");
inboxStyle.setSummaryText("summarytext");
// fetch push messages
synchronized (mPushMessages) {
    HashMap<String, PushMessage> messages = mPushMessages.get(key);
    if (messages != null) {
        for (Entry<String, PushMessage> msg : messages.entrySet()) {
            inboxStyle.addLine(Html.fromHtml("at least <b>one word</b> should be bold!");
        }
        builder.setStyle(inboxStyle);
        builder.setNumber(messages.size());
    }
}

对此有什么想法吗?

推荐答案

你不需要使用 fromHtml.我过去曾遇到过 fromHtml 问题(当您显示的内容来自用户时,代码注入可能会导致丑陋的事情).另外,我不喜欢在 strings.xml 中放置格式化元素(如果您使用翻译服务,它们可能会搞砸您的 HTML 标签).

You don't need to use fromHtml. I've had issues with fromHtml in the past (when what you display comes from user, code injection can result in ugly things). Also, I don't like putting formatting elements in strings.xml (if you use services for translation, they might screw up your HTML tags).

addLine 方法,作为在通知中设置文本的大多数方法(setTickersetContentInfosetContentTitle、等)以 CharSequence 作为参数.

The addLine method, as most methods to set text in notifications (setTicker, setContentInfo, setContentTitle, etc.) take a CharSequence as parameter.

所以你可以传递一个Spannable.假设您想要粗体这个和斜体那个.",你可以这样格式化(当然不要硬编码位置):

So you can pass a Spannable. Let's say you want "Bold this and italic that.", you can format it this way (of course don't hardcode positions):

Spannable sb = new SpannableString("Bold this and italic that.");
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 14, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);

现在,如果您需要使用本地化字符串动态构建字符串,例如今天是 [DAY],早上好!",请在 strings.xml:

Now if you need to build string dynamically with localized strings, like "Today is [DAY], good morning!", put string with a placeholder in strings.xml:

<string name="notification_line_format">Today is %1$s, good morning!</string>

然后这样格式化:

String today = "Sunday";
String lineFormat = context.getString(R.string.notification_line_format);
int lineParamStartPos = lineFormat.indexOf("%1$s");
if (lineParamStartPos < 0) {
  throw new InvalidParameterException("Something's wrong with your string! LINT could have caught that.");
}
String lineFormatted = context.getString(R.string.notification_line_format, today);

Spannable sb = new SpannableString(lineFormatted);
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), lineParamStartPos, lineParamStartPos + today.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);

您会看到今天是星期日,早上好!",据我所知,它适用于所有版本的 Android.

You'll get "Today is Sunday, good morning!", and as far as I know it works with all versions of Android.

这篇关于样式通知 InboxStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆