Android多行通知/带有较长文本的通知

24
我需要创建一个包含更长文本的通知,这可行吗?默认情况下不行,但您可以使用自定义布局,这就是我所做的。现在我可以显示多行文本,但是您可以看到,文本仍然被打断/没有完全显示?)请问有人能告诉我我做错了什么/通知大小是否有固定限制?如果您查看屏幕截图,您会注意到还有很多空间...感谢任何提示!

顺便说一句,这是用于自定义布局的 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>

Longer notification text


1
尝试在所有视图上使用layout_height="wrap_content" - Felix
谢谢,但这并没有改变任何事情 ): - stefan.at.kotlin
据我所知,您无法将通知大小调整为屏幕截图中显示的默认大小,但我可能是错误的。 - Tom O
只是一个想法。在文本视图中添加滚动视图是否可行? - Mahadevan Sreenivasan
至少不要使用remoteview和xml,因为remoteview不支持scrollview。来源:https://dev59.com/mVLTa4cB1Zd3GeqPaGQ3以及当我尝试时收到的错误消息;-)只是想知道是否可以手动/通过代码构建布局? - stefan.at.kotlin
6个回答

65
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


51

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

5

通知视图的高度被限制在65sp。这是实现细节,没有记录,并且在 Android 4.1 中已更改为支持可展开通知。因此,请不要依赖于此特定值,而是依赖于视图具有有限的高度。

这里是用于填充通知区域视图的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>

我并不是不相信你,但你有官方(安卓文档)来源吗?只是想知道我可以在哪里查找。谢谢! - stefan.at.kotlin
谢谢提供源代码,太棒了!(感谢提供源代码,而不是它的限制:P) - stefan.at.kotlin
限制高度似乎是有道理的。因为任何第三方应用程序都可以通过将其高度增加到某个任意值来滥用通知区域。在这种情况下,通知的可用性会大大降低,而所有人都会责怪Android而不是应用程序。 - inazaruk
我认为这个想法总体上是有道理的,但我认为应该向用户提供更多信息/更长的文本。你可以只显示一个预览,然后展开单个通知,再折叠它或类似的操作。 - stefan.at.kotlin
我认为通知的想法是展示应用程序中更多信息的方式。你为什么不创建一些自定义的对话框/活动,当用户点击时启动,并在其中完全显示你的消息?我认为这就是NotificationManager开发人员所考虑的模式。 - inazaruk
1
@stefan.at.wfp - 你可以通过比全屏活动更少的干扰来显示与你的通知相关的意图。简单的方法是使用Theme.Translucent,这样你的活动只显示你想要的内容,留下完整的可见性。 - mah

2

这是我在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));

1

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

从您提供的页面中可以看到:

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

但是,您可以显示多个通知、'sticky'通知或者通知内部的滚动文本。

有关通知的更多信息,请参见:

NotificationNotification Builder


我和你的想法一样,但我仍在寻找这种行为的“官方来源”。滚动文本,这是个好主意 :-) 我会研究一下的,谢谢! - stefan.at.kotlin
1
请注意,Notification.Builder 仅适用于 Android 3.0 及以上版本。 - Felix
1
嗯,依我之见,它并没有明确说明限制的方式(高度/大小?)。文档可以更具体一些... - stefan.at.kotlin

1

Android提供了一个可扩展的通知视图,支持三种样式:大图片样式、收件箱样式、大文本样式(256 dp),但仅适用于Android版本高于Jelly Bean。对于低版本,我们没有任何大文本样式通知。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接