如何强制通知以多行显示文本?
Posted
技术标签:
【中文标题】如何强制通知以多行显示文本?【英文标题】:How can I force a notification to display text in multiple lines? 【发布时间】:2015-10-26 08:40:04 【问题描述】:我的状态栏通知消息太长,所以我想多行显示它,但我不知道为什么它不这样显示。
这是我的代码:
String msg = "text1 text2 text3 text4 text5";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(context.getResources().getString(R.string.time_to_brush_title));
mBuilder.setContentText(msg);
mBuilder.setTicker(context.getResources().getString(R.string.time_to_brush_title));
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
但是,这是通知栏包含的内容:
text1 text2 text3 ...
【问题讨论】:
【参考方案1】:如果你想在 4.1 中使用这种通知>然后使用
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++)
inboxStyle.addLine(events[i]);
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
这是参考链接https://developer.android.com/guide/topics/ui/notifiers/notifications.html
【讨论】:
***.com/questions/20748049/…的可能重复 是的,在我发布这个答案之前我没有看到所以发布以上是关于如何强制通知以多行显示文本?的主要内容,如果未能解决你的问题,请参考以下文章