Android 5+自定义通知XML布局与RemoteViews,为ImageButton设置正确的图标色调

20
我的应用程序使用了自定义的 Notification 布局,其中包含 RemoteViews。
为了显示文本,布局使用以下系统样式:
- android:TextAppearance.Material.Notification.Title - android:TextAppearance.Material.Notification
这个功能运行良好。然而,TextAppearance 样式不能用于设置 android:tint 的值,因此我不得不硬编码颜色。
据我所知,没有特殊的系统样式可以用于设置通知 ImageButton 的着色。
在当前的 Android 5+ 系统上,硬编码的颜色工作正常,但是一些用户安装了自定义 ROM 和自定义深色主题,通知的外观会有问题,即黑色图标在黑色背景上。
有没有办法从 XML 布局中获取系统通知图标/ImageButton 颜色并应用它?
或者也许有其他方法可以实现这一点?

1
你想要更新位于custom_notification_layout中的ImageButton,还是想要更新出现在顶部通知栏上的通知图标? - Nilesh Deokar
1
请添加您自定义的通知布局。 - JAAD
1
也许这篇文章可以帮助你实现你所要求的功能:https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color - Sudheesh R
3
@NileshDeokar 这个问题是关于自定义通知布局的,是的。我仍然没有想出解决该问题的好方法,并且后来还遇到了一些与此相关的新问题。我也没有修复它,但我计划设计2-3种不同的自定义布局,并使用硬编码颜色,然后分析当前的颜色方案,基于那个使用不同颜色的布局。 - Alexey Yakovenko
@AlexeyYakovenko,你成功让通知工作了吗?在我的应用程序中,我使用系统样式android:TextAppearance.Material.Notification.Titleandroid:TextAppearance.Material.Notification.Time,但在华为Y6 II上,它会在黑色背景上显示黑色文本。因此,即使使用系统样式也不能保证您的通知看起来正确。真是太遗憾了... - Mikalai Daronin
6个回答

2
抱歉,根据我的了解,定制ROM具有单独的系统设计和配置,并且它们不是官方版本。因此,如果没有关于其设计的知识,就无法支持定制ROM。而Android API是用于支持官方ROM的。希望这可以帮到您!

1
感谢您发布这个问题。这是唯一正确的答案。 - Alexey Yakovenko
很高兴能帮助你 :) - Animesh Mangla

2
尝试这个例子:

通知的定义:

// Declare variable
public static Bitmap icon;

// Assign value but this line can be different depends on current 
// android sdk vesion ...
icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.YOUR_IMAGE);

     mBuilder = new NotificationCompat.Builder(this);
     mBuilder.setShowWhen(false);
     mBuilder.setDefaults(Notification.DEFAULT_ALL);
     mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
     mBuilder.setSmallIcon(R.drawable.image1); // One way to load img
     mBuilder.setContentText("this text not visible");
     mBuilder.setLargeIcon(icon);// This is the line
     mBuilder.setPriority(Notification.PRIORITY_DEFAULT);
     mBuilder.setContent(contentNotifySmall); // ORI
     //mBuilder.setAutoCancel(false);
     mBuilder.setCustomBigContentView(contentNotify);

我为任何情况设置了小型和大型变体,这很重要。


1

对于背景,您可以尝试使用以下属性...

app:backgroundTint="@color/pay"

---------Or-------------

android:tint="@color/white"

1
您可以从 TextAppearance_Compat_Notification_Title 中获取 textColor,并将其作为色调以编程方式添加到图像中,如下所示。
val remoteViews = RemoteViews(service.packageName, R.layout.notification_layout)

val icon = Icon.createWithResource(context, R.drawable.icon)
val attrs = intArrayOf(android.R.attr.textColor)
with(context.obtainStyledAttributes(R.style.TextAppearance_Compat_Notification_Title, attrs)) {
    val iconColor = getColor(0, Color.GRAY)
    icon.setTint(iconColor)
    recycle()
}
remoteViews.setImageViewIcon(R.id.ibPlayPause, icon)

0

你可以使用 notificationlistenerservice 来获取已激活的通知。这将返回 StatusBarNotifications 的列表,然后只需要:

StatusBarNotifcation sBNotification = activeNotifications[0];
Notification notification = sBNotification.getNotification();
int argbColor = notification.color;

0
如果您在布局中将ImageButton更改为ImageView,您可以使用以下内容更新它:
    RemoteViews remoteView = getRemoteViews(context); 
     // load base icon from res
    Bitmap baseIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.base_icon);
     // edit Bitmap baseIcon any way for your choose
    Bitmap editedBitmap = ...
    // update notification view with edited Bitmap
    remoteView.setImageViewBitmap(R.id.button_icon, edited);

顺便提一下,你可以像这样编辑位图: https://dev59.com/HW025IYBdhLWcg3w9qyQ#5935686 或者其他任何方式

希望能有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接