Implementation method: Google DayNight
- Dependencies: You only need appCompat-related dependencies in your project.
For example,Implementation 'androidx. Appcompat: appcompat: 1.0.2'
- Modify the theme
Change the theme toTheme.AppCompat.DayNight
Or its subtopics. - increase
values-night
File plus, store corresponding to the night modecolor.xml
File. (Color name must be consistent) - increase
drawable-night-hdpi
Or is itmipmap-night-hdpi
Folder, xhdPI, xxhdPI if any. Put in the icon of the corresponding page mode. (The icon name must be the same) - An activity inherits from AppCompatActivity.
- Switching mode 1 (Rebuilding Activity to Take effect)
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
The switch at nightgetDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Switch the day
- 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
-