造型通知InboxStyle [英] Styling notification InboxStyle

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

问题描述

我尝试实现一个可扩展的通知,我已经使用了<一href="http://developer.android.com/reference/android/support/v4/app/NotificationCompat.InboxStyle.html">InboxStyle为

根据从<一个下面的图片上href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html#BigNotify">documentation:

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

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

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

这个你知道吗?

解决方案

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

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

所以,你可以通过一个 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

 &LT;字符串名称=notification_line_format&GT;!今天是%1 $ S,早上好&LT; /串&GT;
 

然后格式化是这样的:

 今天字符串=星期天;
字符串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.

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

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