样式通知InboxStyle

时间:2013-01-30 10:43:34

标签: android notifications

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

基于documentation的以下图片:

inboxstyle notification

应该可以设置文本样式。在这种情况下,请将“Google Play”加粗。

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

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

对此有何想法?

5 个答案:

答案 0 :(得分:48)

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

addLine方法,因为大多数在通知中设置文字的方法(setTickersetContentInfosetContentTitle等)都以CharSequence为参数

所以你可以通过Spannable。假设你想要“大胆这个和斜体那个。”,你可以这样格式化(当然不要硬编码位置):

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中添加带占位符的字符串:

<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。

答案 1 :(得分:7)

您的代码在我的Samsung S3上使用Android 4.1.1就像魅力一样。您使用的是哪个Android版本?

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.Html;
import android.view.Menu;

public class MainActivity extends Activity {

    private final int ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.addLine(Html
                .fromHtml("<i>italic</i> <b>bold</b> text works"));
        mBuilder.setStyle(inboxStyle);
        mBuilder.setNumber(1);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(ID, mBuilder.build());
    }

}

答案 2 :(得分:7)

我为此做了一个帮助方法:

private final StyleSpan mBoldSpan = new StyleSpan(Typeface.BOLD);

private SpannableString makeNotificationLine(String title, String text) {
    final SpannableString spannableString;
    if (title != null && title.length() > 0) {
        spannableString = new SpannableString(String.format("%s  %s", title, text));
        spannableString.setSpan(mBoldSpan, 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        spannableString = new SpannableString(text);
    }
    return spannableString;
}

像这样使用:

inboxStyle.addLine(makeNotificationLine("UserXYZ", "How are you?"));

答案 3 :(得分:3)

这是最简单的解决方案

它不使用InboxStyle,因为在我的情况下我不需要它

CharSequence boldUsernameMessage = Html.fromHtml("<b>@" + username +"</b> " + message);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
    .setContentText(boldUsernameMessage)
    .setStyle(new NotificationCompat.BigTextStyle().bigText(boldUsernameMessage)) // Makes it expandable to show the message
    .build();

<强>结果

enter image description here

答案 4 :(得分:2)

对于粗体文字,您应使用<strong>标记。您的代码没有任何问题。只需将<b>更改为<strong>

编辑: 您应该将代码更改为:

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 <strong>one word</strong> should be bold!");
        }
        builder.setStyle(inboxStyle);
        builder.setNumber(messages.size());
    }
}