我正在尝试使用两个自定义视图构建可扩展通知
在互联网上搜索说我需要使用这样的代码
val r = RemoteViews(activity.packageName, R.layout.cf_watcher_notification_small)
val r2 = RemoteViews(activity.packageName, R.layout.cf_watcher_notification_big)
val n = NotificationCompat.Builder(activity, "test").apply {
setSmallIcon(R.drawable.ic_develop)
setNotificationSilent()
setShowWhen(false)
setAutoCancel(true)
setCustomContentView(r)
setCustomBigContentView(r2)
setStyle(NotificationCompat.DecoratedCustomViewStyle())
}
但是生成的通知没有展开按钮,可以将折叠视图切换为大视图,反之亦然,当我按下通知时,什么也没有发生
没有大内容视图的代码
setCustomContentView(r)
//setCustomBigContentView(r2)
setStyle(NotificationCompat.DecoratedCustomViewStyle())
结果符合预期
仅具有大内容视图的代码
//setCustomContentView(r)
setCustomBigContentView(r2)
setStyle(NotificationCompat.DecoratedCustomViewStyle())
结果通知用我需要的按钮
如何获得这个按钮?
您必须使用
NotificationCompat.BigTextStyle
使其可扩展。
示例-
var notification = NotificationCompat.Builder(activity, "test")
.setSmallIcon(R.drawable.ic_develop)
.setNotificationSilent()
.setShowWhen(false)
.setAutoCancel(true)
.setCustomContentView(r)
.setCustomBigContentView(r2)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(SOME_TEXT))
.build()
您可以在这里
获取更多信息您可以在自定义布局中添加一个按钮,并通过为该按钮编写单击操作来展开和折叠通知,并使视图消失并可见