样式通知InboxStyle

War*_*ith 30 notifications android

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

根据文档中的以下图像:

收件箱通知

应该可以设置文本样式.在这种情况下,将"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());
    }
}
Run Code Online (Sandbox Code Playgroud)

对此有何想法?

tde*_*aux 49

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

addLine作为最方法的方法,来设置通知文本(setTicker,setContentInfo,setContentTitle等)采取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);
Run Code Online (Sandbox Code Playgroud)

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

<string name="notification_line_format">Today is %1$s, good morning!</string>
Run Code Online (Sandbox Code Playgroud)

然后格式化:

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);
Run Code Online (Sandbox Code Playgroud)

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


riw*_*nyk 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());
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 我可以深入挖掘并发现问题在我身边:我只想设置消息的一部分,这意味着我只在字符串的一部分上使用了`fromHtml()`.我用`fromHtml("<b>粗体</ b>")+"其他一些文字"`结果没有造型.将其余字符串移动到`fromHtml()`调用可以完美无缺!谢谢! (3认同)

Yur*_*raj 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;
}
Run Code Online (Sandbox Code Playgroud)

使用这样:

inboxStyle.addLine(makeNotificationLine("UserXYZ", "How are you?"));
Run Code Online (Sandbox Code Playgroud)