android - 更改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>
解决方案
推荐阅读
- r - 多个图形命令在 R 图中不起作用?
- python - python kivy 背景颜色变白
- flutter - 颤振改变底部导航栏高度
- suse - 如何在 SLES 12 上安装 podman
- r - 使用 ggplot2 基于两个因子变量(在 x 轴上)对箱线图进行排序
- nestjs - 如何使用 nginx 在数字海洋中运行nestjs?
- python - pytorch 中是否有 tfagents 的替代品
- windows - 如何以编程方式更改地理位置 Windows10?
- php - 警告:无法修改标头信息 php 中的错误
- composer-php - PHP Composer 打印帮助消息而不是运行命令