Changing notification icon background on Lollipop

2019-02-02 18:59发布

I was going through the Notifications design pattern, and didn't find anything that talks about notification icon background. As you probably noticed, there is only a light grey background for custom notifications. But apps like Hangouts, or simply the USB Debugging notification has a custom color for their notification icon background.

Is there any possibility to change that grey into something else? (that specific circle's color programmatically)

See picture

3条回答
我想做一个坏孩纸
2楼-- · 2019-02-02 19:25

1) Obtain Color

int color = 0xff123456;
int color = getResources().getColor(R.color.my_notif_color);
int color = ContextCompat.getColor(context, R.color.my_notif_color);

2) Set the Color to the Notification

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
...
builder.setColor(color);
Notification notif = builder.build();

The color is respected only on Lollipop and only affects background of the small icon. If a large icon is shown its contents are entirely your responsibility.

Source: NotificationCompat.Builder#setColor(int)

查看更多
Fickle 薄情
3楼-- · 2019-02-02 19:36

getColor(int) has been deprecated on Resources

We should now use one of these alternatives:

查看更多
看我几分像从前
4楼-- · 2019-02-02 19:40

if you've defined color in colors.xml then in your NotificationBuilder add value as

.setColor(getResources().getColor(R.color.<YOUR_COLOR>))

That should solve your problem. It only affect to background of the icon.

查看更多
登录 后发表回答