# Android10 Black theme fit
AndroidTenAdaptiveDemo
Adaptation idea:
- Follow the system Settings to automatically adapt the DarkTheme
- Application system level automatic adaptation
- Manually switch the day and night mode in the App
- Configuration adaptation The solution ADAPTS itself
# Follow the system Settings to automatically adapt the DarkTheme
Android 10 provides Force Dark functionality. Android :forceDarkAllowed=”true” android:forceDarkAllowed=”true” android:forceDarkAllowed=”true” Android :forceDarkAllowed=”true”
<! -- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<! -- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:forceDarkAllowed">true</item>
</style>
Copy the code
Manually switch the day and night mode in the App
class ThemeHelper {
companion object {
// Bright theme
const val LIGHT_MODE = "light"
// Black theme
const val DARK_MODE = "dark"
// The default theme
const val DEFAULT_MODE = "default"
/** * Application theme *@param themePref
*/
fun applyTheme(@NonNull themePref: String) {
when (themePref) {
LIGHT_MODE -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
DARK_MODE -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
DEFAULT_MODE -> {
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
// Follow the system Theme to switch the Theme
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
// Automatically switch according to the powerAppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY); }}}}/** * Check whether dark themes are enabled */
fun isNightMode(context: Context): Boolean {
return (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
}
}
}
Copy the code
Configure the adaptation scheme
- theme
light
- theme
night
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="color_FFFFFF">#FFFFFF</color>
<color name="color_000000"># 000000</color>
themes.xml
<color name="tv_font_color">@color/color_000000</color>
themes.xml night
<color name="tv_font_color">@color/color_FFFFFF</color>
Copy the code