您如何更改通知操作按钮的图标?

问题描述 投票:2回答:2

[当您使用.addAction(int图标,CharSequence标题,PendingIntent intent)构建通知并添加操作按钮时,是否有办法在按下时更改图标?

android android-notifications
2个回答
0
投票

要实时更改操作图标,您需要设置具有相同标题和新图标的新操作,然后重新启动通知。例如:

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
startForeground(101, notificationBuilder);

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(101, notificationBuilder);

-1
投票

您需要创建一个如下所示的drawable,并将其提供给addAction

button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_pressed="true"/>
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_focused="true"/>
  <item android:drawable="@drawable/actionbarcontacts_off"/>
</selector>

addAction(R.drawable.button, <title>, <intent>);

© www.soinside.com 2019 - 2024. All rights reserved.