我需要在安卓系统中创建一个自定义通知,而不是使用默认的通知。目前的通知包含如下图所示的图标、标题和信息:
我希望将其自定义为如下所示的样式:
请问如何实现这个功能?
![enter image description here](https://istack.dev59.com/YICgd.webp)
![enter image description here](https://istack.dev59.com/2yHFP.webp)
通知视图
普通视图 - 普通视图中的通知将出现在高度最多为64 dp的区域内。即使您创建了带有大视图样式的通知,它也将在未展开之前以普通视图的形式出现。
Content title
Large icon
Content text
Content info
Small icon
Notification time
正常视图
大型视图 - 通知的大型视图只在通知被展开时出现,这发生在通知在通知抽屉的顶部或用户使用手势扩展通知时。扩展通知首次在Android 4.1 JellyBean [API 16]中引入。可扩展的通知旨在支持称为Notification.Style的丰富的通知样式对象。
大型视图示例
访问此链接expandable-notifications-android
更多信息请参阅官方文档docs
// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFractory.decodeResource(
getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
这个被称为“扩展布局”,从Jelly Bean版本开始就可用,详见Android通知设计模式。
通知有2种视图:
当通知被展开时,即通知在通知抽屉的顶部,或用户通过手势展开通知时,通知的大视图才会出现。扩展通知是在Android 4.1 JellyBean [API 16]中首次引入的。
在您的情况下,您只想在通知中显示大图片,请尝试使用Notification.BigPictureStyle:
Bitmap remote_picture = null;
// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");
try {
remote_picture = BitmapFactory.decodeStream(
(InputStream) new URL(sample_url).getContent());
} catch (IOException e) {
e.printStackTrace();
}
// Add the big picture to the style.
notiStyle.bigPicture(remote_picture);
您可以使用 NotificationCompat 需要 RemoteView。以下是我设置的自定义布局名称:
activity_downlaodactivity
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.activity_downlaodactivity);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new android.support.v4.app.NotificationCompat.Builder(this)
.setContent(contentView)
manager.notify(0, mBuilder.build());