android - 带有远程视图的 Android 10 上的黑暗通知
问题描述
当我在 Android 10 中将显示模式更改为深色时,根据此处显示的图像获取深色(黑色)通知。我尝试更改文本颜色,但它没有重新调整。我正在使用远程视图来创建自定义通知。如何为深色模式和浅色模式更改不同的远程内容视图文本颜色?这是我的代码:
val contentViewBig = RemoteViews(context.applicationContext.packageName, R.layout.design_custom_notification)
contentViewBig.setTextViewText(R.id.tvNotificationTitle, notificationTitle)
contentViewBig.setTextViewText(R.id.tvNotificationBody, notificationBody)
val nId = Random().nextInt()
val pendingIntent = PendingIntent.getActivity(context,
nId /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val channelId = context.getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setCustomContentView(contentViewBig)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLights(Color.WHITE, 2000, 3000)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
context.resources.getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH)
channel.description = context.resources.getString(R.string.app_name)
channel.setShowBadge(true)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager?.createNotificationChannel(channel)
}
if (notificationManager != null) {
notificationManager.notify(nId, notificationBuilder.build())
wakeLockScreen(context)
}
解决方案
我通过创建values-night文件夹解决了问题。我在values-night文件夹中添加了 colors.xml 并覆盖了我在正常值中使用的颜色- colors.xml并在远程视图设计文件中应用了文本颜色。
参考。https://medium.cobeisfresh.com/how-to-implement-day-night-mode-in-your-android-app-2f21907f9b0a
更新:
在黑暗模式下防止最新Xiaomi devices
力量黑暗应用程序设计的另一件事。
将以下参数theme.xml
应用于您的主题风格。
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
推荐阅读
- docker - 如何使运行 Windows 主机的 docker 容器中的端口可用于本地网络
- python - 如何根据熊猫另一列中的条件生成具有值的新列
- javascript - 如何在不单击任何按钮并获取用户响应的情况下从 C# 代码后面显示带有是和否选项的弹出窗口
- swift - 如何在 iOS 中将 Firebase 推送通知从设备发送到设备
- list - 如何通过列表
剑道网格读取() - dictionary - Delphi TDictionary 与泛型
- powershell - 使用 Powershell 编辑 IIS 自定义响应标头
- xslt - 如何在 XSLT 的 for 循环中获取下一个节点的子值
- java - 如何将包含“e +”字符串转换为长字符串?
- c# - 如何对OwnsMany 集合进行分页?