首页 > 解决方案 > 更改Android通知颜色的正确方法是什么?

问题描述

我正在尝试自定义 Android 通知的背景颜色

在此处输入图像描述

如您所见,我希望将红色标记标记的整个区域的背景更改为红色。还有一些灰色空间。如何解决这个问题?

这是我的代码,顺便说一句(在运行 Android 12 的 Pixel 3 上测试):

NotificationUtil.kt

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import kotlin.random.Random

class NotificationUtil (private val context: Context) {

    fun showNotification(title: String, message: String) {
        val intent = Intent(context, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(
            context, 0, intent,
            PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE
        )

        val channelId = context.getString(R.string.default_notification_channel_id)
        val remoteView = RemoteViews(context.packageName,  R.layout. custom_notification_layout )
        val notificationBuilder = NotificationCompat.Builder(context, channelId)
            .setColor(ContextCompat.getColor(context, android.R.color.holo_red_dark))
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
        notificationBuilder.setContent(remoteView)

        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                "Default Channel",
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(Random.nextInt(), notificationBuilder.build())
    }

}

custom_notification_layout.xml

<?xml version = "1.0" encoding = "utf-8" ?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:id = "@+id/layout"
    android:layout_width = "fill_parent"
    android:layout_height = "64dp"
    android:background = "@color/colorAccent"
    android:padding = "10dp" >

    <ImageView
        android:id = "@+id/image"
        android:layout_width = "wrap_content"
        android:layout_height = "fill_parent"
        android:layout_alignParentStart = "true"
        android:layout_marginEnd = "10dp"
        android:src = "@mipmap/ic_launcher" />

    <TextView
        android:id = "@+id/title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_toEndOf = "@id/image"
        android:textColor = "#000"
        android:textSize = "13sp" />

    <TextView
        android:id = "@+id/text"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_below = "@id/title"
        android:layout_toEndOf = "@id/image"
        android:ellipsize = "marquee"
        android:singleLine = "true"
        android:textColor = "#000"
        android:textSize = "13sp" />
</RelativeLayout>

标签: androidandroid-notifications

解决方案


推荐阅读