【问题标题】:Night Mode color is not applying to the recycler view background夜间模式颜色不适用于回收站视图背景
【发布时间】:2019-10-21 07:16:02
【问题描述】:

我想为我的 android 应用程序实现夜间模式,所以我使用 Theme.AppCompat.DayNight 主题来实现夜间模式。 但我必须在夜间模式下自定义工具栏和回收站视图的颜色。

为此,我在 attrs.xml 文件中声明了该属性,并将该属性用作 recyclerview 中的背景。

这里是 attrs.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ds">
        <attr name="rv_color" format="color"/>
    </declare-styleable>
</resources>

这里是回收站视图

 <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/rv_color"
        android:overScrollMode="never"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

现在对于样式,我已经为夜间模式声明了 styles.xml 和 styles.xml(夜间)。

这里是styles.xml

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@android:color/white</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/colorPrimary</item>
    <item name="android:windowDisablePreview">false</item>
    <item name="rv_color">#FF0000</item>
</style>

这里是styles.xml(晚上)

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@android:color/white</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/colorPrimary</item>
    <item name="android:windowDisablePreview">false</item>
    <item name="rv_color">#FFFF00</item>
</style>

在styles.xml 文件中,我为recyclerview 背景定义了红色,在夜间模式文件中定义了黄色。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

上面的行存在于夜间模式的活动文件中。

但每次回收站视图颜色为红色时,即来自 styles.xml

为什么styles.xml(夜间)颜色不适用于recyclerview。

为什么它不起作用?或任何其他方式来做到这一点?

【问题讨论】:

  • 只有rv_color 不起作用!?
  • 是的,其他参数值是从夜间模式自动设置的,例如卡片背景、文本颜色等。
  • 但我必须自定义它。
  • @RohitSuthar 嘿,你找到解决办法了吗?

标签: android android-recyclerview android-styles android-night-mode android-dark-theme


【解决方案1】:

我遇到了同样的问题(只是 RecyclerView 总是有 style.xml 中定义的颜色(夜间),无论是否激活了白天或夜间模式)并在 this thread 中找到了解决方案;我唯一需要改变的是不要打电话

getApplicationContext() 

但要使用

MyActivity.this 

而不是在使用

创建适配器时
MyAdapter adapter = new MyAdapter(getApplicationContext(), arrayList);

在对应的Activity中。工作解决方案:

MyAdapter adapter = new MyAdapter(MyActivity.this, arrayList);

【讨论】:

  • 我遇到这个问题已经一个小时了,这是唯一有效的解决方案。谢谢!
  • 非常感谢您的解决方案,我尝试了很多天来找到更改主题的解决方案,即使我不确定如何在 StackOverflow 上发布此问题以使人们了解我遇到的错误,
  • 天哪,你救了我这么多年痛苦的调试,非常感谢!!
【解决方案2】:

另一种方法是在您的资源中创建一个 values-night 文件夹,在其中放置一个 colors.xml 并声明一个颜色,如“rv_color”:

<color name="rv_color">#ffff00</color>

在常规值/colors.xml 中:

<color name="rv_color">#ff0000</color>

将回收站视图的颜色设置为该颜色。无需声明任何自定义属性。

【讨论】:

  • 我在两个文件中都定义了颜色,但它不起作用。每次它从colors.xml应用颜色,而不是从colors.xml(晚上)。
  • stackoverflow.com/a/65066768/4676291 请查看这些 cmets。
【解决方案3】:

当我第一次尝试实施 ondzilla 的建议时,它没有奏效。但是,当我将 recyclerview 背景设置为 color.xml 文件中已有的另一种颜色时,背景现在可以在白天/黑夜之间正确切换。然后我将它切换回 rv_color 并且它工作正常。

不知道这对你的问题是否有帮助,但我很高兴你问了这个问题,而且我的问题现在可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多