Android 视图 setBackgroundColor() 和夜间模式

问题描述

我正在开发一个 android 应用程序,我需要允许用户通过颜色选择器选择列表视图项目的背景颜色。每个列表视图项都是一个 LinearLayout,我以这种方式在我的适配器中设置背景颜色:

holder.rowLayout.setBackgroundColor(this.backgroundColor!!)

在白天模式下一切正常,但当应用切换到夜间模式时,我遇到了颜色问题。

我们以这个例子为例: 在日间模式下,我选择亮绿色背景,它正确应用于列表:

enter image description here

但是当我切换到夜间模式时,颜色如下:

enter image description here

现在,我不想对所有应用程序禁用白天\黑夜。 我的所有主题都响应白天/夜晚的偏好,我只想避免使用暗模式来更改此列表视图的颜色,因为是用户选择其背景颜色,而我不希望 Android 更改它。

我尝试将较旧的主题应用于线性布局并使用 LinearLayoutCompat 没有运气。 有人可以帮助我吗?

解决方法

试试这个,在 XML 代码对象输入那一行:

android:forceDarkAllowed="false"