如何使用新的 DayNight 主题? [英] How to use the new DayNight Theme?

查看:37
本文介绍了如何使用新的 DayNight 主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始接触 Google 支持库的新更新,我想在我的应用中实施 Theme.AppCompat.DayNight.
我遇到的问题是似乎没有人解释如何自定义它.因此,如果我想白天使用不同的 colorAccent,晚上使用不同的 colorAccent,我该怎么做?您是否应该指定不同的明暗主题作为基础?提前致谢!

Starting to get my hands into that new update to the Google Support Library and I want to implement the Theme.AppCompat.DayNight into my app.
The problem I am having is that it seems no one explained how to customize it. So If I want to have a different colorAccent for day and a different one for night, how do I do that? Are you supposed to specify different dark and light themes to base off of? Thanks in advance!

推荐答案

您可以使用 夜间资源限定符文件夹.
通过这种方式,您可以为深色(夜晚)和浅色主题(白天)定义颜色和其他资源.

You can use the night resource qualifier folder.
In this way you can define colors and the other resources for the dark (night) and for the light theme (day).

限定词:
night:夜间时间
notnight:白天时间

为了支持带有 Material Components Theme 的深色主题,请使用:

In order to support the dark theme with a Material Components Theme use:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

使用 AppCompat 主题:

  <style name="AppTheme" parent="Theme.AppCompat.DayNight">
      <item name="colorPrimary">@color/primary</item>
   </style>

然后在您的应用主题中定义引用颜色资源,并在需要时覆盖values-night 目录中的值:

Then define in your app theme the references color resources, and override the value in the values-night directory if needed:

示例:resvaluescolors.xml:

   <color name="colorPrimary">.....</color>

resvalues-nightcolors.xml 文件夹中定义相同的颜色:

In resvalues-nightcolors.xml folders define the same color:

   <color name="colorPrimary">.....</color>

这篇关于如何使用新的 DayNight 主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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