改变在棒棒糖的通知象背景

时间:2014-12-07 13:39:26

标签: android android-notifications android-5.0-lollipop

我正在浏览Notifications design pattern,并没有找到任何谈论通知图标背景的内容。您可能已经注意到,自定义通知只有浅灰色背景。但是环聊等应用或简单的USB调试通知都会为其通知图标背景提供自定义颜色。

有没有可能将灰色变成其他东西? (以编程方式显示特定圆圈的颜色)

See picture

3 个答案:

答案 0 :(得分:152)

1)获得颜色

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

2)将颜色设置为通知

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

颜色仅在Lollipop上受到尊重,仅影响小图标的背景。如果显示大图标,则其内容完全由您负责。

来源:NotificationCompat.Builder#setColor(int)

答案 1 :(得分:9)

如果您已在colors.xml中定义了颜色,则在NotificationBuilder中将值添加为

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

那应该可以解决你的问题。 它只影响图标的背景。

答案 2 :(得分:2)

getColor(int)已弃用Resources

我们现在应该使用其中一种替代方案:

  • Resources的{​​{3}}
  • ContextCompat的{​​{3}}