夜间模式颜色不适用于回收者视图背景 [英] Night Mode color is not applying to the recycler view background

查看:111
本文介绍了夜间模式颜色不适用于回收者视图背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to implement night mode for my android app so I have used Theme.AppCompat.DayNight theme for implementing Night Mode. But I have to customize the color of the Toolbar and recycler view during the Night Mode.

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

For that, I have declared the attribute in the attrs.xml file and use that attribute as a background in the recyclerview.

这是attrs.xml文件

Here is the attrs.xml file

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

这是recyclerview

Here is the recyclerview

 <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">

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

Now for styles, I have declared styles.xml and styles.xml (night) for Night Mode.

这是styles.xml

Here is the 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(晚上)

Here is the styles.xml (night)

<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背景定义了红色,而在夜间模式文件中定义了黄色.

In the styles.xml file I have defined RED color for recyclerview background and YELLOW color in the night mode file.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

以上一行显示在夜间模式"的活动"文件中.

但是每次回收器视图的颜色都是红色的,即来自styles.xml

But Every time the recycler view color is RED i.e. from the styles.xml

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

Why styles.xml (night) color is not applying to the recyclerview.

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

Why it is not working? or any other way to do this?

推荐答案

我遇到了同样的问题(只是RecyclerView始终具有styles.xml(夜间)中定义的颜色,而不管白天还是夜间模式都被激活了)并在此线程中找到了解决方案;我唯一需要更改的就是不要致电

I had the same problem (only that the RecyclerView always had the color defined in styles.xml (night), regardless of whether day or night mode was activated) and found a solution in this thread; the only thing I had to change was not to call

getApplicationContext() 

但要使用

MyActivity.this 

相反,当使用

MyAdapter adapter = new MyAdapter(getApplicationContext(), arrayList);

在相应的活动中.工作解决方案:

in the corresponding Activity. Working solution:

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

这篇关于夜间模式颜色不适用于回收者视图背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆