【问题标题】:Android notification InboxStyle() with colored string带有彩色字符串的 Android 通知 InboxStyle()
【发布时间】:2015-08-09 22:56:41
【问题描述】:

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

但没有运气... :(

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

【问题讨论】:

    标签: android string colors notifications spannable


    【解决方案1】:

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

     spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    更新

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

    【讨论】:

    • @diggin 这很奇怪,因为它对我有用。查找我的问题,我对其进行了更新并添加了示例
    • Google 应该默认添加字体颜色/样式。
    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多