造型通知InboxStyle造型、通知、InboxStyle

由网友(识趣 Content つ)分享简介:我尝试实现一个可扩展的通知,我已经使用了InboxStyle为根据从documentation:应该是可能的样式文本。在这种情况下,使谷歌播放大胆。只有InboxStyle具有 addLine()在那里我可以传递的CharSequence。我试着用 Html.fromHtml()和使用了一些HTML格式,但我不能成功。...

我尝试实现一个可扩展的通知,我已经使用了InboxStyle为

根据从documentation:

应该是可能的样式文本。在这种情况下,使谷歌播放大胆。

只有InboxStyle具有 addLine()在那里我可以传递的CharSequence。我试着用 Html.fromHtml()和使用了一些HTML格式,但我不能成功。

  NotificationCompat.InboxStyle inboxStyle =新NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(标题);
inboxStyle.setSummaryText(summarytext);
//获取推送消息
同步(mPushMessages){
    HashMap的<字符串,PushMessage>消息= mPushMessages.get(密钥);
    如果(消息!= NULL){
        对于(进入<字符串,PushMessage> MSG:messages.entrySet()){
            inboxStyle.addLine(Html.fromHtml(至少< B>一个字< / B>要大胆!);
        }
        builder.setStyle(inboxStyle);
        builder.setNumber(messages.size());
    }
}
 

这个你知道吗?

解决方案 我的专高一 Day03 Notification通知

您不需要使用 fromHtml 。我已经在过去(当你显示的内容来自用户,code注射液可导致丑陋的东西)与 fromHtml 的问题。还有,我不喜欢把格式化元素的strings.xml (如果你使用的服务进行翻译,他们可能会搞砸了你的HTML标记)。

addLine 的方法,因为大多数的方法来设置的通知文本(setTicker , setContentInfo setContentTitle 等)采取的CharSequence 作为参数。

所以,你可以通过一个 Spannable 。比方说,你想粗体这和斜体的这一点。,你可以格式化它这种方式(当然,不要硬code位置):

  Spannable SB =新SpannableString(粗体这和斜体的。);
sb.setSpan(新StyleSpan(android.graphics.Typeface.BOLD),0,4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(新StyleSpan(android.graphics.Typeface.ITALIC),14,20,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(某人);
 

现在,如果你需要使用本地化字符串动态创建的字符串,如今天是 [日] ,早上好!,把字符串中的占位符的strings.xml

 <字符串名称=notification_line_format>!今天是%1 $ S,早上好< /串>
 

然后格式化是这样的:

 今天字符串=星期天;
字符串lineFormat = context.getString(R.string.notification_line_format);
INT lineParamStartPos = lineFormat.indexOf(%1 $ S);
如果(lineParamStartPos℃,){
  抛出新的抛出:InvalidParameterException(!什么是错的与你的字符串林特就能赶上的。);
}
字符串lineFormatted = context.getString(R.string.notification_line_format今天);

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

您会得到今天是日,早上好!,而据我所知,这适用于所有版本的Andr​​oid。

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

Based on the following image from the documentation:

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

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());
    }
}

Any idea about this?

解决方案

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

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

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);

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>

Then format this way:

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);

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

阅读全文

相关推荐

最新文章