Java Android 颜色通知图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45874742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Color Notification Icon
提问by Oblivionkey3
I'm working on an app where I create a notification for the user. I want the icon to appear as white when it's in the status bar, but colored blue when it's being displayed in the drop down notification menu. Here's an example of the same thing being done by the Google Store app.
我正在开发一个为用户创建通知的应用程序。我希望图标在状态栏中显示为白色,但在下拉通知菜单中显示时显示为蓝色。以下是 Google 商店应用执行相同操作的示例。
White notification in status bar:
状态栏中的白色通知:
Colored notification in drop down menu:
下拉菜单中的彩色通知:
How can I replicate this? What properties do I have to set?
我怎样才能复制这个?我必须设置哪些属性?
Edit:Here's my current code - I made the image all white with a transparent background, so it looks fine in the status bar, but in the notification drop, the image is still the same white color:
编辑:这是我当前的代码 - 我将图像全部设为白色并带有透明背景,因此在状态栏中看起来不错,但在通知下拉菜单中,图像仍然是相同的白色:
private NotificationCompat.Builder getNotificationBuilder() {
return new NotificationCompat.Builder(mainActivity)
.setDeleteIntent(deletedPendingIntent)
.setContentIntent(startChatPendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.skylight_notification)
.setColor(ContextCompat.getColor(mainActivity, R.color.colorPrimary))
.setContentTitle(mainActivity.getString(R.string.notification_title))
.setContentText(mainActivity.getString(R.string.notification_prompt));
}
采纳答案by Oblivionkey3
I found the answer to my question here: https://stackoverflow.com/a/44950197/4394594
我在这里找到了我的问题的答案:https: //stackoverflow.com/a/44950197/4394594
I don't know entirely what the problem was, but by putting the huge png that I was using for the icon into the this tool https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notificationand by placing the generated icons it gave into my mipmap folder, I was able to get the setColor(...)
property to work correctly.
我不完全知道问题是什么,但是通过将我用于图标的巨大 png 放入这个工具https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type= image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notification并将生成的图标放入我的 mipmap 文件夹中,我能够使该setColor(...)
属性正常工作。
回答by Advice-Dog
When building the notification, you can set the color and the icon. If your icon is a pure white image, it'll apply the color for you in the correct spots.
在构建通知时,您可以设置颜色和图标。如果您的图标是纯白色图像,它会在正确的位置为您应用颜色。
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())
}
回答by Umar Hussain
You can use the DrawableCompat.setTint(int drawable);
of the drawable before setting the drawable.
And do mutate()
the drawable otherwise the color tint will be applied to every instance of that drawable
您可以DrawableCompat.setTint(int drawable);
在设置可绘制对象之前使用可绘制对象的 。并执行mutate()
可绘制,否则颜色将应用于该可绘制的每个实例
回答by JRG
Here is what I did for my app ...
这是我为我的应用程序所做的......
private void showNotification(Context context) {
Log.d(MainActivity.APP_TAG, "Displaying Notification");
Intent activityIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setColor(Color.GREEN);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentTitle("EarthQuakeAlert");
mBuilder.setContentText("It's been a while you have checked out earthquake data!");
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
Sample With Color:
带颜色的样品:
回答by radu_paun
For firebase nofitications sent from console you just need to add this in your manifest:
对于从控制台发送的 firebase 通知,您只需在清单中添加以下内容:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/white_logo" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/custom_color" />
Where white_logo is your app white logo, and custom_color is the color you want to have the icon and text colored.
其中 white_logo 是您的应用程序白色徽标, custom_color 是您希望图标和文本着色的颜色。
More details here: https://firebase.google.com/docs/cloud-messaging/android/client
更多细节在这里:https: //firebase.google.com/docs/cloud-messaging/android/client
回答by Pradeep Kumar
If you want to change color and title name as per gmail and twitter in Push notification or inbuilt notification then you need to add these lines in notification.
如果您想在推送通知或内置通知中根据 gmail 和 twitter 更改颜色和标题名称,则需要在通知中添加这些行。
builder.setSmallIcon(R.drawable.skull)
builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
First line used for icon and in second line you need to define color
第一行用于图标,第二行你需要定义颜色