2013-05-09 80 views
23

我的Android应用程序必须能够发送短的警报了一大群人。明显的地方是在通知中心。完整的通知显示在代码中没有问题,但在通知中心,用户只能看到前几个单词,然后看到一个省略号。通知时间不长,最多只有10-15个字。我怎样才能让文字换到新的一行?Android的通知 - 显示完整的邮件

我的代码来构建通知是在这里

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.splash) 
    .setContentTitle("Student Engauge") 
    .setContentText(extras.getString("message")) 
    .setAutoCancel(true) 
    .setTicker(extras.getString("message")); 
    final int notificationId = 1; 
    NotificationManager nm = (NotificationManager) getApplicationContext() 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    nm.notify(notificationId, mBuilder.build()); 

回答

42

要显示大块文本,使用BigTextStyle。以下是BigTextStyle中给出的示例代码。此通知将包含一行文本,如果需要,将会扩展到更多行。

Notification noti = new Notification.Builder() 
.setContentTitle("New mail from " + sender.toString()) 
.setContentText(subject) 
.setSmallIcon(R.drawable.new_mail) 
.setLargeIcon(aBitmap) 
.setStyle(new Notification.BigTextStyle() 
    .bigText(aVeryLongString)) 
.build(); 

支持Android库

Notification noti = new Notification.Builder() 
.setContentTitle("New mail from " + sender.toString()) 
.setContentText(subject) 
.setSmallIcon(R.drawable.new_mail) 
.setLargeIcon(aBitmap) 
.setStyle(new NotificationCompat.BigTextStyle() 
    .bigText(aVeryLongString)) 
.build(); 
+0

次大不前4.1支持自定义通知布局的限制更多的信息,我想的东西,更支持 – centree 2013-05-09 21:15:58

+7

'的setStyle(新NotificationCompat.BigTextStyle()。bigText(aVeryLongString))' – likejiujitsu 2014-03-11 16:40:55

+0

@centree使用NotificationCompat支持较旧设备上较新的通知功能。 http://developer.android.com/reference/android/support/v4/app/NotificationCompat.html – pmont 2014-04-05 07:25:16

4

对于Android 4.1及更高版本的设备,大的观点是最合适的解决方案,显示大量的文本。对于4.1之前的设备,您可以使用自定义通知布局来显示更多数据,如提到here。但是你应该记住两两件事:

  1. official documentation

    注意:当您使用自定义通知的布局,要特别小心,以确保您的自定义布局可与不同的设备取向和分辨率。虽然此建议适用于所有View布局,但对于通知而言尤其重要,因为通知抽屉中的空间非常有限。不要让你的自定义布局太复杂,并且一定要以各种配置进行测试。

  2. 自定义通知的布局有一定的局限性。太长的文本没有完整显示,但10-15个单词可能适合自定义布局。 This answer有大约