android 自定义通知消息设置背景色不生效,导致部分机型显示白色字体,白色文字,用户以为不显示
16lz
2021-01-23
为什么要自定义 notification
android 自带的通知控件一般不符合公司要求,所以需要自定义notification。
怎么自定义
自定义时需要使用 RemoteViews
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, "TestMess"); // 一定要设置小图标 .setSmallIcon(R.mipmap.ic_launcher_round) 否则会崩溃 Notification notification = notificationBuilder .setSmallIcon(R.mipmap.ic_launcher_round) .setAutoCancel(true).build(); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notification_layout); notification.contentView = views; notification.contentView.setViewVisibility(R.id.ll_review, View.VISIBLE);// 注意要把notification 的背景隐藏,否则部分机型不生效,设置的布局文件的背景色不生效 notification.contentView.setViewVisibility(R.id.notification_background, View.GONE); // 图标 Bitmap headBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher); views.setImageViewBitmap(R.id.imageview_notifi_head, headBitmap); // 标题 views.setTextViewText(R.id.textview_notifi_title, "testTitle"); // 内容 views.setTextViewText(R.id.textview_notifi_chat_content, "testContent"); // 时间 views.setTextViewText(R.id.textview_notifi_time, "testTime");
容易忽略
注意要把notification 的背景隐藏,否则部分机型不生效,设置的布局文件的背景色不生效
notification.contentView.setViewVisibility(R.id.notification_background, View.GONE);将 notification_layout.xml 布局文件中 在对应的 view 上设置颜色值和大小
8.0通知不显示
android 8.0 以后对通知栏有了渠道的概念,同一个渠道会自动折叠。但需要适配否则不显示
// notificationBuilder.setChannelId(String channelId); 一般使用 packageName 可以保证唯一性 notificationBuilder.setChannelId(context.getPackageName());
更多相关文章
- Android 解决WebView无法上传文件的问题
- android 设置apk文件默认安装sdcard中
- Android studio 真机调试时生成的文件找不到的解决方案
- Android 应用软件开发(八)窗口布局
- 在Android中通过Java修改文件权限
- Android开发便签9:在android资源文件中定义字符串数组
- java/android 统计文件夹大小及删除文件夹下所有文件和路径
- Android七大布局
- Android:使用SAX或者DOM或者pull操作XML文件