【问题标题】:Android multiline notifications / notifications with longer textAndroid 多行通知/带有较长文本的通知
【发布时间】:2011-06-15 12:09:27
【问题描述】:

我需要创建一个带有更长文本的通知,可以吗?默认情况下不是,但您可以使用custom layout,这就是我所做的。现在我可以显示多行,但正如您所见,文本仍然损坏/未完全显示? ):有人可以告诉我我做错了什么/通知的大小是否有固定限制?如果您查看屏幕截图,您会注意到,还有很多空间...感谢您的任何提示!

顺便说一句,这是用于自定义布局的 XML,基于 http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="3dp"
          >
<ImageView android:id="@+id/image"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_marginRight="10dp"
          />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#000"
          />
</LinearLayout>

【问题讨论】:

  • 尝试在所有视图上使用layout_height="wrap_content"
  • 谢谢,但这不会改变任何事情):
  • 据我所知,您无法将通知设置为大于屏幕截图中显示的默认大小,但我可能错了。
  • 只是一个想法。向文本视图添加滚动视图有效吗?
  • 至少不使用 remoteview 和 xml,因为 remoteview 不支持滚动视图):来源:stackoverflow.com/questions/4351459/… 以及我尝试它时收到的错误消息 ;-) 只是想知道我是否可以构建手动/按代码布局?

标签: android notifications


【解决方案1】:
NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(title)
                    .setStyle(new NotificationCompat.BigTextStyle()
                                      .bigText(message))
                    .setContentText(message)                                            
                    .setDefaults(NotificationCompat.DEFAULT_SOUND)
                    .setContentIntent(contentIntent)
                    .setAutoCancel(true);

mNotificationManager.notify(requestID, mBuilder.build());

曾经引用https://developer.android.com/guide/topics/ui/notifiers/notifications.html

【讨论】:

    【解决方案2】:

    对于 Jelly Bean 及更高版本,您可以使用可扩展通知。最简单的方法是使用 NotificationCompat.BigTextStyle 作为通知。

    像这样:

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(getString(R.string.title));
    bigTextStyle.bigText(getString(R.string.long_explanation));
    
    mBuilder.setStyle(bigTextStyle);
    

    【讨论】:

      【解决方案3】:

      通知视图的高度受到65sp 的限制。这是实现细节,没有记录在案,has been changed in Android 4.1 to support expandable notifications。所以不要依赖这个特定的值,而是依赖于视图高度有限的事实。

      这里是status_bar_latest_event.xml,用于在通知区域中膨胀视图:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="65sp"
          android:orientation="vertical"
          >
      
          <com.android.server.status.LatestItemView android:id="@+id/content"
                  android:layout_width="match_parent"
                  android:layout_height="64sp"
                  android:background="@drawable/status_bar_item_background"
                  android:focusable="true"
                  android:clickable="true"
                  android:paddingRight="6sp"
                  >
          </com.android.server.status.LatestItemView>
      
          <View
              android:layout_width="match_parent"
              android:layout_height="1sp"
              android:background="@drawable/divider_horizontal_bright"
              />
      
      </LinearLayout>
      

      【讨论】:

      • 不是我不相信你,但你有官方(android 文档)来源吗?只是想知道我在哪里可以查到。谢谢!
      • 感谢来源,太好了! (来源,而不是它是有限的事实:P)
      • :) 限制高度是有道理的。因为任何第三方应用程序都可以通过将通知区域的高度增加到某个任意值来滥用通知区域。在这种情况下,通知的可用性将急剧下降,每个人都会责怪 Android,而不是应用程序。
      • 我同意一般来说这是有道理的,但我认为应该可以向用户提供更多信息/更长的文本。您可以只显示预览,然后您必须展开单个通知并可以再次折叠它或某事。像这样。
      • @stefan.at.wfp - 您可以让与通知相关联的意图显示此信息,并且比全屏活动更少侵入。执行此操作的简单方法是使用 Theme.Translucent,以便您的活动只显示您想要的内容,并在其后面留下完整的可见性。
      【解决方案4】:

      这在 5.0 上对我有用 它很好地包裹了长线。它还允许您提供一个字符串数组,这些字符串将以新行分隔显示。

              String[] multilineDescription = new String[] { "line 1", "another very long line that will get wrapped." };
      
              NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext)
                      .setSmallIcon(smallIcon)
                      .setContentTitle(title)
                      .setContentText("Pull down for more information");
      
              String description;
              if (null == multilineDescription || multilineDescription.length == 0) {
                  description = "No more information.";
              } else {
                  description = multilineDescription[0];
      
                  for (int i=1; i<multilineDescription.length; i++) {
                      description += System.getProperty("line.separator");
                      description += multilineDescription[i];
                  }
              }
      
              builder.setStyle(new NotificationCompat.BigTextStyle().bigText(description));
      

      【讨论】:

        【解决方案5】:

        我的理解是,Android 的通知系统对每个通知的高度都有限制,以避免单个通知填满屏幕。

        从您链接的页面:

        注意:当您使用自定义通知布局时,请特别注意确保您的自定义布局适用于不同的设备方向和分辨率。虽然此建议适用于所有视图布局,但它对于通知尤其重要,因为通知抽屉中的空间非常有限。不要让您的自定义布局过于复杂,并确保在各种配置中对其进行测试。

        但是,您可以显示多个通知、“粘性”通知或通知内的滚动文本。

        有关通知功能的更多信息,请参阅:

        NotificationNotification Builder

        【讨论】:

        • 我假设和你一样,但是我仍在寻找这种行为的“官方来源”。嗯滚动文本,这是一个想法 :-) 会调查这个,谢谢!
        • 请注意Notification.Builder 仅适用于 Android 3.0 及更高版本。
        • 嗯嗯,在我看来,它并没有真正说明它在哪些方面(高度/大小?)受到限制?文档可能更具体...
        【解决方案6】:

        Android 提供大视图可扩展通知,它们支持 3 种样式,大图片样式,收件箱样式,大文本样式 (256 dp),但仅限于大于果冻豆的 android 版本。对于较低版本,我们没有任何大的文本样式通知。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-26
          相关资源
          最近更新 更多