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

Zso*_*sár 77 android android-notifications android-5.0-lollipop

我正在通过通知设计模式,并没有找到任何谈论通知图标背景.您可能已经注意到,自定义通知只有浅灰色背景.但是环聊等应用或简单的USB调试通知都会为其通知图标背景提供自定义颜色.

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

见图

Eug*_*nec 153

1)获得颜色

int color = 0xff123456;
int color = getResources().getColor(R.color.my_notif_color);
int color = ContextCompat.getColor(context, R.color.my_notif_color);
Run Code Online (Sandbox Code Playgroud)

2)将颜色设置为通知

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
...
builder.setColor(color);
Notification notif = builder.build();
Run Code Online (Sandbox Code Playgroud)

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

来源:NotificationCompat.Builder #setColor(int)

  • 根据这个:http://stackoverflow.com/a/27023679/327011 setColor只会改变小图标的颜色.谨防. (11认同)
  • 1)这仅适用于`LOLLIPOP`.2)这不会影响你在`setLargeBitmap`中提供的图像.它只颜色小图标的背景. (5认同)

小智 9

如果您在colors.xml中定义了颜色,那么在NotificationBuilder中添加值为

.setColor(getResources().getColor(R.color.<YOUR_COLOR>))
Run Code Online (Sandbox Code Playgroud)

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