You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android 12系统中修改通知的背景颜色

安卓本地通知自定义背景实现方案

要通过NotificationCompat.Builder实现通知背景自定义,可根据需求选择以下两种适配性较高的方案:

方案1:系统原生着色(改动最小,适配性最优)

直接调用NotificationCompat.Builder的setColor()方法传入目标色值,系统会自动将通知的主题背景区域渲染为指定颜色,无需自定义布局,绝大多数原生系统和第三方ROM都能正常适配:

// Kotlin 示例代码
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("通知标题")
    .setContentText("通知内容")
    // 此处设置你需要的通知主题色
    .setColor(ContextCompat.getColor(context, R.color.your_target_color))
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Java 示例代码
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("通知标题")
    .setContentText("通知内容")
    .setColor(ContextCompat.getColor(context, R.color.your_target_color))
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

方案2:完全自定义布局(可实现全背景自定义)

如果需要替换整个通知的背景色,可通过自定义RemoteViews布局实现:

  1. 首先创建自定义通知布局文件layout/custom_notification.xml,在根布局设置目标背景色:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    <!-- 此处设置你需要的整体背景色 -->
    android:background="@color/your_target_background_color"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/notification_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:layout_marginTop="8dp"/>

</LinearLayout>
  1. 在NotificationCompat.Builder中绑定自定义布局:
// Kotlin 示例代码
val customView = RemoteViews(packageName, R.layout.custom_notification)
// 给自定义布局的控件赋值
customView.setTextViewText(R.id.notification_title, "自定义标题")
customView.setTextViewText(R.id.notification_content, "自定义通知内容")

val builder = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    // 增加该配置可保留系统通知的边距适配,减少不同ROM的兼容问题
    .setStyle(NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(customView)
    // 如需适配大尺寸展开通知,可追加setCustomBigContentView配置
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)

注意事项

  • Android 8.0及以上系统发送通知前必须先创建对应ID的通知渠道,否则通知无法正常显示
  • 自定义布局时注意适配深色模式,保证文字和背景的对比度符合系统要求,避免出现内容看不清的问题
  • 部分第三方ROM会限制自定义通知的样式,建议在目标机型上提前测试适配效果

内容的提问来源于stack exchange,提问作者Aegletes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 16:15:02