【问题标题】:Android 5+ custom notification XML layout with RemoteViews, set correct icon tint for ImageButton带有 RemoteViews 的 Android 5+ 自定义通知 XML 布局,为 ImageButton 设置正确的图标色调
【发布时间】:2023-03-26 17:02:02
【问题描述】:

我的应用正在使用带有 RemoteViews 的自定义通知布局。

为了显示文本,布局使用以下系统样式:

android:TextAppearance.Material.Notification.Title android:TextAppearance.Material.Notification

这很好用。

但是,TextAppearance 样式不能用于设置android:tint 的值,所以我不得不硬编码颜色。

据我所知,设置通知 ImageButton 色调没有特殊的系统样式。

硬编码颜色在当前的 Android 5+ 系统上运行良好,但一些用户安装了自定义深色主题的自定义 ROM,通知看起来不正确,即黑色背景上的黑色图标。

有没有办法获取系统通知图标/图像按钮的颜色,并从 XML 布局中应用它?

或者也许还有其他方法可以实现这一点?

【问题讨论】:

  • 您要更新custom_notification_layout 中的ImageButton 还是要更新顶部通知栏上显示的通知图标?
  • 请添加您的自定义通知布局
  • 也许这会让你有一个想法来实现你所要求的:snow.dog/blog/…
  • @NileshDeokar 问题是关于自定义通知布局的,是的。我仍然没有想出任何好的解决方案来解决这个问题,后来我遇到了一些与该问题相关的新问题。我也没有解决这个问题,但我打算用硬编码的颜色设计 2-3 种不同的自定义布局,然后分析当前的颜色方案,然后根据它使用不同颜色的布局。
  • @AlexeyYakovenko 您是否设法使您的通知正常工作?在我的应用程序中,我使用系统样式android:TextAppearance.Material.Notification.Titleandroid:TextAppearance.Material.Notification.Time,但它在华为 Y6 II 上会在黑色背景上显示黑色文本。因此,即使使用系统样式也不能保证您的通知看起来正确。真可惜……

标签: android xml notifications


【解决方案1】:

抱歉,但据我所知,自定义 ROM 具有单独的系统设计、配置,而且也不是官方的。

因此,在不了解其设计的情况下支持自定义 ROM 是不可能的。 安卓 API 用于支持官方 ROM。

希望对你有帮助!!

【讨论】:

  • 感谢您发布此信息。这是唯一正确的答案。
  • 很高兴为您提供帮助:)
【解决方案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);

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

【讨论】:

    【解决方案3】:

    背景可以试试这些属性吗...

    app:backgroundTint="@color/pay"
    

    ---------或者-------------

    android:tint="@color/white"
    

    【讨论】:

      【解决方案4】:

      您可以从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)
      

      【讨论】:

        【解决方案5】:

        您可以使用notificationlistenerservice 来获取活动通知。这会返回一个 StatusBarNotifications 列表,然后只需:

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

        【讨论】:

          【解决方案6】:

          如果您在布局中将 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://stackoverflow.com/a/5935686/7630175 或任何其他方式

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-07-28
            • 2013-10-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-16
            相关资源
            最近更新 更多