【问题标题】:How to automatically switch to dark mode on Android app?如何在 Android 应用上自动切换到暗模式?
【发布时间】:2019-12-02 06:58:50
【问题描述】:

我正在制作一个 Android 应用。我为黑暗模式制作了另一个 UI。所以这就是我需要的;该应用程序将在当地时间自动切换到深色主题。例如,当太阳下山时,应用程序将切换到暗模式。

或者另一种选择是在一天中的预设时间切换到暗模式。希望你能理解我的问题。如果有人知道,请帮助我,如果可能的话,我更喜欢第一个选项。提前致谢。

【问题讨论】:

标签: android android-studio user-interface android-dark-theme


【解决方案1】:

也许你可以看看AppCompatDelegate.setDefaultNightMode()

您只需使用 DayNight 的父级定义您的主题:

<style name="MyTheme" parent="Theme.AppCompat.DayNight">    
<!-- Blah blah -->
</style>

每个样式都有:

<style name="Theme.AppCompat.DayNight" 
       parent="Theme.AppCompat.Light" />

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

然后您可以拨打:AppCompatDelegate.setDefaultNightMode()

其中之一:

MODE_NIGHT_NO. Always use the day (light) theme.
MODE_NIGHT_YES. Always use the night (dark) theme.
MODE_NIGHT_FOLLOW_SYSTEM (default). This setting follows the system’s setting, which on Android Q and above is a system setting (more on this below).
MODE_NIGHT_AUTO_BATTERY. Changes to dark when the device has its ‘Battery Saver’ feature enabled, light otherwise.
MODE_NIGHT_AUTO_TIME & MODE_NIGHT_AUTO. Changes between day/night based on the time of day.

您通常会在自己的自定义应用程序类中执行此操作:

public class MyApplication extends Application {

    public void onCreate() {
        super.onCreate();        
        AppCompatDelegate.setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_YES);
    }
}

更多信息here

【讨论】:

    【解决方案2】:

    快捷方式:

    public class MainActivity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //restore preferences
            SharedPreferences settings0 = this.getSharedPreferences(PREFS_NAME, 0);
            lightMode = settings0.getBoolean("key0", true);
    
            //retrieve selected mode
            if (lightMode) {
    
                //light mode
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
            } else {
    
                //dark mode
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            }
    
            Switch switch0 = findViewById(R.id.Switch0);
            switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                    if (darkMode) {
    
                        text = "Mode: light";
    
                        //light mode
                        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
                        darkMode = false;
    
                    } else {
    
                        text = "Mode: dark";
    
                        //dark mode
                        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
                        darkMode = true;
                    }
    
                    //save music preferences
                    SharedPreferences setting0 = getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor0 = setting0.edit();
                    editor0.putBoolean("key0", darkMode);
                    editor0.apply();
                }
            });
    }
    

    【讨论】:

    • 其实还有一种更好的方法来制作暗模式。您可以创建一个开关来更改明暗模式的背景颜色。
    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 2020-10-20
    • 1970-01-01
    • 2020-07-07
    • 2022-11-12
    • 2022-01-04
    • 2021-08-19
    • 1970-01-01
    相关资源
    最近更新 更多