创建自定义通知布局

如需让通知在不同版本的 Android 上呈现最佳效果, 请使用标准通知模板来构建通知。如果您 想在通知中提供更多内容,请考虑使用 可展开的通知模板之一。

不过,如果系统模板无法满足您的需求,您可以为通知使用自己的布局。

为内容区域创建自定义布局

如果您需要自定义布局,可以将 NotificationCompat.DecoratedCustomViewStyle 应用于通知。借助此 API,您可以为通常由标题和文本内容占据的内容区域提供自定义布局,同时仍为通知图标、时间戳、副文本和操作按钮使用系统装饰。

此 API 的工作方式与可展开的通知模板类似,它基于 基本通知布局构建,如下所示:

  1. 使用 NotificationCompat.Builder 构建 基本通知。
  2. 调用 setStyle(),并向其传递 NotificationCompat.DecoratedCustomViewStyle 的实例。
  3. 将自定义布局作为 RemoteViews 的实例进行扩充。
  4. 调用 setCustomContentView(),为收起的 通知设置布局。
  5. (可选)还可以调用 setCustomBigContentView(),为展开的通知设置不同的 布局。

准备布局

您需要一个 small 和一个 large 布局。在此示例中,small 布局可能如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Small notification, showing only a title" />
</LinearLayout>

large 布局可能如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Large notification, showing a title and a body." />

    <TextView
        android:id="@+id/notification_body"
        style="@style/TextAppearance.Compat.Notification.Line2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is the body. The height is manually forced to 300dp." />
</LinearLayout>

构建和显示通知

布局准备就绪后,您可以按以下示例所示使用它们:

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Get the layouts to use in the custom notification.
val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)

// Apply the layouts to the notification.
val customNotification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setStyle(NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(notificationLayout)
    .setCustomBigContentView(notificationLayoutExpanded)
    .build()

notificationManager.notify(666, customNotification)

请注意,通知的背景颜色可能因设备和版本而异。在自定义布局中,为文本应用支持库样式(例如 TextAppearance_Compat_Notification),为标题应用支持库样式(例如 TextAppearance_Compat_Notification_Title), 如以下示例所示。这些样式会适应颜色变化,因此您不会遇到黑底黑字或白底白字的情况。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="@string/notification_title"
    android:id="@+id/notification_title"
    style="@style/TextAppearance.Compat.Notification.Title" />

避免在 RemoteViews 对象上设置背景图片,因为这可能会导致文本无法辨认。

当用户在使用应用时触发通知时,结果类似于图 1:

收起的自定义通知布局
图 1.在使用其他应用时,系统会显示一个小型通知布局 。

点按展开箭头可展开通知,如图 2 所示:

展开后的自定义通知布局
图 2.在使用其他应用时,系统会显示一个大型通知布局。

通知超时时间结束后,通知仅在系统栏中可见,如图 3 所示:

系统栏中的收起通知
图 3.小型通知布局 在系统栏中的显示方式。

点按展开箭头可展开通知,如图 4 所示:

系统栏中的展开式通知
图 4.大型通知布局在 系统栏中的显示方式。

创建完全自定义的通知布局

如果您不希望通知使用标准通知 图标和标头进行装饰,请按照前面的步骤操作,但 不要 调用 setStyle()。