创建自定义通知布局

为了确保您的通知在不同版本的 Android 上都能呈现最佳视觉效果,请使用标准通知模板来构建通知。如果您想在通知中提供更多内容,请考虑使用可展开通知模板

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

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

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

此 API 的工作方式与可展开通知模板类似,它是通过在基本通知布局上构建来实现的,步骤如下:

  1. 使用 NotificationCompat.Builder 构建基本通知
  2. 调用 setStyle(),并传入 NotificationCompat.DecoratedCustomViewStyle 的实例。
  3. 将您的自定义布局渲染(inflate)为 RemoteViews 的实例。
  4. 调用 setCustomContentView() 为折叠状态的通知设置布局。
  5. (可选)调用 setCustomBigContentView() 为展开状态的通知设置不同的布局。

准备布局

您需要一个 small(小)布局和一个 large(大)布局。在本例中,small 布局可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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 布局可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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>

构建并显示通知

准备好布局后,您可以按照以下示例使用它们:

Kotlin

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)

Java

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

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

// Apply the layouts to the notification.
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(new 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:

An image showing a collapsed notification
图 1. 使用其他应用时显示的小通知布局。

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

An image showing an expanded notification in the system bar
图 2. 使用其他应用时显示的大通知布局。

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

An image showing a collapsed notification in the system bar
图 3. 系统栏中显示的小通知布局。

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

An image showing an expanded notification in the system bar
图 4. 系统栏中显示的大通知布局。

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

如果您不希望通知带有标准的通知图标和标题栏,请按照上述步骤操作,但不要调用 setStyle()