Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

android - How to use the new DayNight Theme?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Qualifiers:
night: Night time
notnight: Day time

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

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

With an AppCompat theme:

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

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

Example: resvaluescolors.xml:

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...