如何在Android N中更改通知操作文本的颜色?

Adi*_*a J 2 android

Android N通知动作

是否可以更改“消防” /“救护” /“警察”的文字颜色?

还是像旧版Android一样向其中添加图标?

Android N前通知操作

Sdg*_*emi 9

要更改操作文本颜色,请使用:

HtmlCompat.fromHtml("<font color=\"" + ContextCompat.getColor(context, R.color.custom_color) + "\">" + context.getString(R.string.fire) + "</font>", HtmlCompat.FROM_HTML_MODE_LEGACY)
Run Code Online (Sandbox Code Playgroud)

作为动作标题CharSequence,例如:

Notification notification = new NotificationCompat.Builder(context, channelId)
        ...
        .addAction(new NotificationCompat.Action.Builder(
                            R.drawable.ic_fire,
                            HtmlCompat.fromHtml("<font color=\"" + ContextCompat.getColor(context, R.color.custom_color) + "\">" + context.getString(R.string.fire) + "</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
                            actionPendingIntent))
                    .build())
        .build();
Run Code Online (Sandbox Code Playgroud)

  • 很奇怪,这是这样做的方式。在 Android 9 上,如果我设置字体颜色,它会更改按钮的背景颜色。 (5认同)
  • 在 Android 8 和 Android 10 上尝试过,它的行为符合预期 (2认同)

Eri*_*icT 7

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary));
Run Code Online (Sandbox Code Playgroud)

NotificationCompat.Builder.setColor 方法用于设置通知的强调颜色,该颜色也将应用于操作按钮的文本。

  • 它还会更改通知的小图标颜色。 (2认同)

Tha*_*man 1

要更改操作的文本颜色并在其左侧放置一个图标,请使用以下示例。

NotificationCompat.Builder(context).setColor(ContextCompat.getColor(context, R.color.yourColorResourceHere))
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Fire", myPendingIntent);
Run Code Online (Sandbox Code Playgroud)