Dark theme configuration change in Android Q

前端 未结 1 566
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 04:41

I want to implement android 10 dark theme in my app , I have these following cases:

SYSTEM_DEFAULT, NIGHT_MODE, LIGHT_MODE

The problem is when I

相关标签:
1条回答
  • 2021-01-06 05:00

    You don't need to set the theme in your activity and recreate it. It's done automatically if you've setup your app theme right.

    To use the Dark in your app, you should extend the DayNight theme as your app theme.

    <style name="AppTheme" parent="Theme.AppCompat.DayNight"> 
    

    or

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    

    If you want for example a different color during dark mode, you should create a 'Android resource directory' called values-night with a 'resource file called colors.xml

    In the colors.xml you can set a different color hex for one of your existing colors.

    For example:

    values/colors.xml contains

    <color name="myColor">#000000</color>
    

    values-night/colors.xml contains

    <color name="myColor">#FFFFFF</color>
    

    EDIT

    You can switch between Dark/light mode in app by calling

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    

    or

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    

    or

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    
    0 讨论(0)
提交回复
热议问题