首页 > 解决方案 > 设置通知栏图标颜色

问题描述

我正在使用 android studio 并希望将所有内容与我的项目颜色相匹配。如何设置通知栏中图标的颜色?或者如果不可能只隐藏图标,而不是栏?

在此处输入图像描述

提前致谢

标签: androidandroid-studio

解决方案


在构建通知时,您可以设置颜色和图标。(如果您的图标是纯白色图像,它会在正确的位置为您应用颜色。)

这是我最近使用的代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationId = 10 // Some unique id.

// Creating a channel - required for O's notifications.
val channel = NotificationChannel("my_channel_01",
        "Channel human-readable title",
        NotificationManager.IMPORTANCE_DEFAULT)

manager.createNotificationChannel(channel)

// Building the notification.
val builder = Notification.Builder(context, channel.id)
builder.setContentTitle("Warning!")
builder.setContentText("This is a bad notification!")
builder.setSmallIcon(R.drawable.skull)
builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
builder.setChannelId(channel.id)

// Posting the notification.
manager.notify(notificationId, builder.build())
}

在第一行检查您手机的版本,并据此获得通知样式

建设者。小图标和builder.set颜色(给你想要的颜色)


推荐阅读