Implementation method: Google DayNight

  1. Dependencies: You only need appCompat-related dependencies in your project.

    For example,Implementation 'androidx. Appcompat: appcompat: 1.0.2'
  2. Modify the theme

    Change the theme toTheme.AppCompat.DayNightOr its subtopics.
  3. increasevalues-nightFile plus, store corresponding to the night modecolor.xmlFile. (Color name must be consistent)
  4. increasedrawable-night-hdpiOr is itmipmap-night-hdpiFolder, xhdPI, xxhdPI if any. Put in the icon of the corresponding page mode. (The icon name must be the same)
  5. An activity inherits from AppCompatActivity.
  6. Switching mode 1 (Rebuilding Activity to Take effect)
    • getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);The switch at night
    • getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO); Switch the day
  7. Switching Mode 2 (effective when the activity is created)
    • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); recreate(); // Switch to daytimeCopy the code
    • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); recreate(); // Switch to nightCopy the code