android– 更改通知RemoteViews背景颜色

android– 更改通知RemoteViews背景颜色,第1张

概述我在使用Application-Theme更改Background-Color时遇到问题.NotificationCompat.BuildernBuilder=newNotificationCompat.Builder(this);TypedValuetypedValue=newTypedValue();getTheme().resolveAttribute(R.attr.colorPrimary,typedValue,true);intiPrimaryC

我在使用Application-theme更改Background-color时遇到问题.

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);TypedValue typedValue = new TypedValue();gettheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);int iPrimarycolor = typedValue.data;gettheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);int iPrimaryDarkcolor = typedValue.data;Intent notintent = new Intent(getApplicationContext(), MainActivity.class);notintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top);PendingIntent notopenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notintent, PendingIntent.FLAG_UPDATE_CURRENT);RemoteVIEws smallContentVIEw = new RemoteVIEws(getPackagename(), R.layout.notification_small);RemoteVIEws bigContentVIEw = new RemoteVIEws(getPackagename(), R.layout.notification_expanded);nBuilder.setSmallicon(R.drawable.not_icon)    .setongoing(true)    .setContentTitle(getCurrentSong().getTitle())    .setContentIntent(notopenOnClick);Notification not = nBuilder.build();smallContentVIEw.setInt(R.ID.not_linLayout, "setBackgroundcolor", iPrimarycolor);smallContentVIEw.setInt(R.ID.not_imvdivIDer, "setBackgroundcolor", iPrimaryDarkcolor);bigContentVIEw.setInt(R.ID.not_linLayout, "setBackgroundcolor", iPrimarycolor);bigContentVIEw.setInt(R.ID.not_imvdivIDer, "setBackgroundcolor", iPrimaryDarkcolor);setListeners(smallContentVIEw);setListeners(bigContentVIEw);not.contentVIEw = smallContentVIEw;not.bigContentVIEw = bigContentVIEw;if (isPlaying()) {    not.contentVIEw.setimageVIEwResource(R.ID.not_btnPlayPause, R.drawable.ic_pause_48dp);    not.bigContentVIEw.setimageVIEwResource(R.ID.not_btnPlayPause, R.drawable.ic_pause_48dp);}else {    not.contentVIEw.setimageVIEwResource(R.ID.not_btnPlayPause, R.drawable.ic_play_48dp);    not.bigContentVIEw.setimageVIEwResource(R.ID.not_btnPlayPause, R.drawable.ic_play_48dp);}

我已经尝试过了,但我的通知背景仍然是白色的.
ID是正确的,VIEw linLayout是linearLayout.

请记住:整个代码在服务中调用!

谢谢!

解决方法:

通过利用NotificationCompat.MediaStyle,可以更轻松地完成大部分工作.它可以在API 24之前的设备上从setColor()调用中获取背景颜色(并在API 24设备上使用该颜色作为重点).这也意味着您不再需要编写任何自定义RemoteVIEws代码,因为它仅依赖于您添加到媒体控件通知中的 *** 作:

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);nBuilder.setSmallicon(R.drawable.not_icon)  .setContentTitle(getCurrentSong().getTitle())  .setContentIntent(notopenOnClick);// This is what sets the background color on <N devices// It is an accent color on N+ devicesnBuilder.setcolor(getResources().getcolor(R.color.colorPrimary));// Add actions via nBuilder.addAction()// Set the style, setShowActionsInCompactVIEw(0) means the first// action you've added will be shown the non-expanded vIEwnBuilder.setStyle(new NotificationCompat.MediaStyle()  .setShowActionsInCompactVIEw(0));

您绝对应该阅读MediaStyle的所有可用方法,并重新阅读Best Practices in media playback I/O 2016 talk以获取有关使用通知的示例代码和最佳实践.具体在30 minutes into the talk

总结

以上是内存溢出为你收集整理的android – 更改通知RemoteViews背景颜色全部内容,希望文章能够帮你解决android – 更改通知RemoteViews背景颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1096126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存