带有彩色字符串的 Android 通知 InboxStyle()

dig*_*gin 2 string notifications android colors spannable

android InboxStyle() 的快速问题,我可以有一个彩色的字符串吗?我尝试使用Spannable

NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
text = "sample";
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text.length(), 0);
style.addLine(spannable);
Run Code Online (Sandbox Code Playgroud)

但没有运气... :(

我可以添加颜色吗?谢谢!

Dim*_*ima 5

在 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE 上更改 setSpan 中的标志

 spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

更新

    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher);
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle("title");
    inboxStyle.setSummaryText("summarytext");
    String lineFormatted = "test";
    Spannable sb = new SpannableString(lineFormatted);
    sb.setSpan(new ForegroundColorSpan(Color.RED), 0, lineFormatted.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    inboxStyle.addLine(sb);
    builder.setStyle(inboxStyle);
    builder.setNumber(1);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, builder.build());
Run Code Online (Sandbox Code Playgroud)

  • @diggin 这很奇怪,因为它的代码对我有用。查找我的问题,我更新了它并添加了示例 (2认同)