【发布时间】:2016-10-27 14:08:47
【问题描述】:
我在使用应用程序主题更改背景颜色时遇到问题。
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
-
好的,但为什么使用 MediaStyle 更好?
-
1) 适用于 API 7+ 设备 2) 使用 Lollipop+ 上的内置样式 3) 自动适应新的 Android N 通知样式 4) 内置支持自定义背景颜色 5) 有内置支持解决在棒棒糖之前的设备上关闭来自前台服务的通知的问题 6) 支持添加您的 MediaSession 令牌以使 Android Wear 控件正常工作。仅举几例。
-
好的,谢谢 :)
标签: android service notifications themes android-remoteview