如何将动画图标添加到下载通知的 contentView?
How to add animated icon to contentView of download notification?
我知道在使用默认 android 资源 android.R.drawable.stat_sys_download
下载文件时,我可以在状态栏中显示动画下载图标。我的问题是如何在下载通知的 contentView 中显示这个动画图标?就像您使用 Chrome.
下载文件时一样
我尝试使用 ImageView 显示资源,但它不支持动画图标,并且图标显示像没有动画的普通图标。
我找到了解决方案。您应该使用通知样式。图标是通知中的主要组件之一,android 设置了它的样式。因此,除了使用 android.R.drawable.stat_sys_download
作为图标资源外,即使您有自定义通知布局,您也应该使用 .setStyle
。
mBuilder = new NotificationCompat.Builder(context, channelID)
.setCustomContentView(collapsedView)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setSmallIcon(android.R.drawable.stat_sys_download)
.setOngoing(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_LOW);
我知道在使用默认 android 资源 android.R.drawable.stat_sys_download
下载文件时,我可以在状态栏中显示动画下载图标。我的问题是如何在下载通知的 contentView 中显示这个动画图标?就像您使用 Chrome.
我尝试使用 ImageView 显示资源,但它不支持动画图标,并且图标显示像没有动画的普通图标。
我找到了解决方案。您应该使用通知样式。图标是通知中的主要组件之一,android 设置了它的样式。因此,除了使用 android.R.drawable.stat_sys_download
作为图标资源外,即使您有自定义通知布局,您也应该使用 .setStyle
。
mBuilder = new NotificationCompat.Builder(context, channelID)
.setCustomContentView(collapsedView)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setSmallIcon(android.R.drawable.stat_sys_download)
.setOngoing(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_LOW);