android - 夜间模式颜色不适用于回收站 View 背景

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

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

为此,我在 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>

这是回收 View

 <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);

以上行出现在夜间模式的 Activity 文件中。

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

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

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

最佳答案

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

getApplicationContext() 

但要使用

MyActivity.this 

而不是在创建适配器时使用

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

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

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

关于android - 夜间模式颜色不适用于回收站 View 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58481456/

相关文章:

android - StaggeredGridLayoutManager 和移动项目

android - fragment 中的主题

android - SetError 在 TextInputLayout 中使用自定义样式产生异常

java - Android-java 条件检查错误 - 需要纠正一些代码逻辑

android - 无法解析 Firebase 身份验证 15.0.0

android - 无法解析 : recyclerview

android - ApplicationContext 或 Activity Context 适合 Adapter 吗?

android - 如何在 Action Bar 中设置粗体标题?

java - Android - 将 Java 类导入 Activity

javascript - 如何保存WebView应用程序的登录信息?