如何使用带有可调文本的图形作为通知动作图标

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

我需要什么

我想要实现的是类似的东西

“

应该可以以编程方式设置图标内显示的数字。即我想要一个带有可通过编程方式调整的文字的通知操作图标。

有没有简单的方法可以实现这一目标?在构建通知时,似乎无法将TextView添加到android drawable并设置文本。还有什么其他方法?

代码

用于构建通知的大致代码是(尽管我认为没有必要回答此问题:]

private void buildNotification() {
    createNotificationChannel();

    Intent playbackActionIntent = new Intent(this, MediaPlayerService.class);
    playbackActionIntent.setAction(ACTION_BACKWARD);
    PendingIntent backwardAction = PendingIntent.getService(this, 3, playbackActionIntent, 0);

    String audioTitle = "testTitle";
    String albumTitle = "testAlbumTitle";

    // Create a new Notification
    mNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            // Hide the timestamp
            .setShowWhen(false)
            // Set the Notification style
            .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                    // Attach our MediaSession token
                    .setMediaSession(mediaSession.getSessionToken())
                    // Show our playback controls in the compat view
                    .setShowActionsInCompactView(0, 1, 2))
            // Set the Notification color
            .setColor(getResources().getColor(R.color.colorAccent))
            .setSmallIcon(R.drawable.ic_notification_new)
            // Set Notification content information
            .setContentText(albumTitle)
            .setContentTitle(audioTitle)
            // Set the visibility for the lock screen
            .setVisibility(VISIBILITY_PUBLIC)
            // Add playback actions
            .addAction(R.drawable.ic_media_backward, "backward", backwardAction)

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager != null) {
        notificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
    }
}

因此,对于R.drawable.ic_media_backward,我希望像链接的图像一样具有可调整的文本的可绘制对象。

感谢您的帮助!到目前为止,我找不到能解决我问题的任何东西。

android android-notifications android-drawable
1个回答
0
投票

请尝试此解决方案:

 mNotificationBuilder .setContentTitle("Exporting...")
                       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
© www.soinside.com 2019 - 2024. All rights reserved.