AppCompat DayNight theme not work on Android 6.0?

后端 未结 6 1317
眼角桃花
眼角桃花 2020-12-09 18:09

I am using the new Theme.AppCompat.DayNight added in Android Support Library 23.2

On Android 5.1 it works well.

On Android 6.0, act

6条回答
  •  暖寄归人
    2020-12-09 18:55

    Google have fix it in support 23.2.1

    Old answer:

    On Android 6.0, system's night mode setting defalut is UiModeManager.MODE_NIGHT_NO, it will change Resources.Configuration.uiMode before onCreate is called. However, support library apply its night mode setting in onCreate in AppCompatActivity, it's too late, I think thats why it not work on 6.0.

    So if we can Override getResources() in AppCompatActivity and change uiMode.

    Old answer:

    Here are code to fix not work on Android 6.0

    public class Application extends android.app.Application {
        static {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_);
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            // add this code for 6.0
            // DO NOT DO THIS. It will trigger a system wide night mode.
            // This is the old answer. Just update appcompat.
            // UiModeManager uiManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
            // uiManager.setNightMode(UiModeManager.MODE_NIGHT_);
        }
    }
    

    Note: If your app don't have location permission, your app will not have the same calculate result of system. It means it is possible support library thinks it is night now when system not, this will cause some of your UI looks dark some light.

    The best way is wait for Google to fix it.

提交回复
热议问题