更改通知RemoteViews背景颜色 [英] Changing Notification RemoteViews Background Color
问题描述
使用应用程序主题"更改背景颜色时遇到问题.
I've a problem changing my Background-Color with the Application-Theme.
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.
I already tried this, but the Background of my Notification is still white. The ID's are correct, the View linLayout is a LinearLayout.
请记住:整个代码是在服务中调用的!
Please keep in mind: The whole codes is invoked in a Service!
谢谢!
推荐答案
通过利用 setColor中提取背景颜色()在API 24之前的设备上调用(并使用该颜色作为API 24+设备上的重点).这也意味着您不再需要编写任何自定义的RemoteViews代码,因为它完全取决于您添加到通知中的媒体控件的操作:
Much of this can be easier done by taking advantage of NotificationCompat.MediaStyle. It pulls the background color from the setColor() call on pre-API 24 devices (and uses that color as an accent on API 24+ devices). This also means you don't need to write any custom RemoteViews code anymore as it relies solely on the actions you add to your notification for media controls:
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+ devices
nBuilder.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 view
nBuilder.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0));
您绝对应该阅读 MediaStyle 并重新获得媒体播放I/O 2016演讲的最佳做法例如有关使用通知的代码和最佳做法.具体在演讲开始30分钟
You should definitely read over all of the methods available for MediaStyle and reivew the Best Practices in media playback I/O 2016 talk for example code and best practices around using notifications. Specifically at 30 minutes into the talk
这篇关于更改通知RemoteViews背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!