【发布时间】: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