修改Android通知背景色的正确方法 自定义布局出现灰色留白如何修复
修复方案
问题根因
Android 12及以上版本对自定义通知引入了系统级背景填充规则,默认未开启自定义颜色填充开关时,自定义布局外的区域会保留系统默认的灰色背景,导致出现留白。
具体修改步骤
- 第一步:给通知构造器添加
setColorized(true)配置,开启自定义颜色填充能力,同时给RemoteViews设置文本内容,替换原有默认的标题、文本配置:
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) // 给自定义布局控件赋值 remoteView.setTextViewText(R.id.title, title) remoteView.setTextViewText(R.id.text, message) val notificationBuilder = NotificationCompat.Builder(context, channelId) .setColor(ContextCompat.getColor(context, android.R.color.holo_red_dark)) .setColorized(true) // 新增:开启自定义颜色填充背景 .setSmallIcon(R.drawable.ic_stat_ic_notification) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setContent(remoteView) val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 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()) } }
- 第二步:调整自定义布局参数,替换废弃属性,适配不同设备的通知高度:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="match_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>
修改完成后重新运行测试,即可实现通知整块区域背景填充为红色的效果。
内容的提问来源于stack exchange,提问作者anta40
相关产品推荐
相关产品推荐

