简体   繁体   English

如何检测手机系统是深色主题还是浅色主题?

[英]How to detect phone system is dark theme or light theme?

Here is the code for the detect dark/light theme when we using the application.这是我们使用应用程序时检测暗/亮主题的代码。

public void onConfigurationChanged(@NonNull Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int mSysThemeConfig = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
    layout = findViewById(R.id.mainLayout);
    switch (mSysThemeConfig) {
        //Light theme
        case Configuration.UI_MODE_NIGHT_NO:
            layout.setBackgroundResource(R.drawable.lightTheme);
            layout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            break;
        //Dark theme
        case Configuration.UI_MODE_NIGHT_YES:
            layout.setBackgroundResource(R.drawable.darkMode);
            layout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            break;
    }
}

How do I detect the dark/light theme when we start an application?启动应用程序时如何检测深色/浅色主题?

I just found it我刚找到

Maybe this can help you也许这可以帮助你

int nightModeFlags =
getContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
     doStuff();
     break;

case Configuration.UI_MODE_NIGHT_NO:
     doStuff();
     break;

case Configuration.UI_MODE_NIGHT_UNDEFINED:
     doStuff();
     break;
}

OR you can go in this link或者您可以在此链接中使用 go

Android - How to detect if night mode is on when using AppCompatDelegate.MODE_NIGHT_AUTO Android - 使用 AppCompatDelegate.MODE_NIGHT_AUTO 时如何检测夜间模式是否开启

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM