为了使通知在不同的 Android 版本上呈现最佳效果,请使用标准通知模板来构建通知。如果您想在通知中提供更多内容,不妨考虑使用某个可展开的通知模板。
不过,如果系统模板无法满足您的需求,您可以使用自己的通知布局。
为内容区域创建自定义布局
如果您需要自定义布局,可以将 NotificationCompat.DecoratedCustomViewStyle
应用于通知。借助此 API,您可以为通常由标题和文本内容占据的内容区域提供自定义布局,同时仍对通知图标、时间戳、子文本和操作按钮使用系统装饰。
此 API 的工作方式与可展开的通知模板类似,都是基于基本通知布局构建,如下所示:
- 使用
NotificationCompat.Builder
构建基本通知。 - 调用
setStyle()
,并向其传递NotificationCompat.DecoratedCustomViewStyle
的实例。 - 将自定义布局膨胀为
RemoteViews
的实例。 - 调用
setCustomContentView()
以设置收起后通知的布局。 - 您还可以选择调用
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:
点按展开箭头可展开通知,如图 2 所示:
通知超时时间结束后,通知仅会显示在系统栏中,如图 3 所示:
点按展开箭头可展开通知,如图 4 所示:
创建完全自定义的通知布局
如果您不想使用标准通知图标和标题装饰通知,请按照上述步骤操作,但不要调用 setStyle()
。