【问题标题】:Android - default dark modeAndroid - 默认暗模式
【发布时间】:2020-12-27 13:25:24
【问题描述】:

我想在我的应用中实现深色模式。默认情况下,我希望它被系统跟踪,所以我在 Main Activity 中放置了:

 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

它工作正常,但如果用户想改变主意并在我的应用程序菜单中选择某个选项来关闭/打开黑暗模式,则活动正在重新启动并且应用程序仍然遵循系统规则。我该如何改变呢?

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_color_mode) {
        if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        else
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
        return true;
    }

【问题讨论】:

  • 请分享您何时设置AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM,是否在onCreate() .. 请与@Zain 重播以得到通知
  • @Zain 感谢您的承诺。负责您提到的选项的代码在onCreate() 内。允许用户更改模式的机制不在onCreate()

标签: java android android-dark-theme


【解决方案1】:

负责您提到的选项的代码位于 onCreate() 中。允许用户更改模式的机制不在 onCreate() 中

public class MainActivityextends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }
}

当您明确更改暗模式时,Android 会重新创建 Activity 并因此再次调用 onCreate

因此,在您更改暗模式后,您不会注意到任何变化,因为当系统调用 onCreate 时会再次调用 AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM

要使其正常工作,您可以将一个值保存到 SharedPreference 中,在设置系统暗模式之前可以在 onCreate 中检查该值。

这可以是一个布尔值,当您想要手动更改暗模式时可以切换它。

这是一个示例

public class MainActivityextends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isSystem = prefs.getBoolean("IS_SYSTEM", true);
        if (isSystem) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        }       
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_color_mode) {
            if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            else
                AppCompatDelegate.setDefaultNightMode(
                        AppCompatDelegate.MODE_NIGHT_YES);
                        
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            prefs.edit().putBoolean("IS_SYSTEM", false).apply();
            return true;
        }   
    
}

更新

效果很好,但是当我退出应用程序然后再次启动时,虽然我已经切换了默认系统模式,但它仍然处于活动状态。这里有可能让它以这种方式工作吗?

您可以使用另一个SharedPreference boolean 来永久保存

public class MainActivityextends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isSystem = prefs.getBoolean("IS_SYSTEM", true);
        boolean isNight = prefs.getBoolean("IS_NIGHT", false);

        if (isSystem) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        } else if (isNight) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }   
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_color_mode) {

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

            if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                prefs.edit().putBoolean("IS_NIGHT", false).apply();
    
            } else {
                AppCompatDelegate.setDefaultNightMode(
                        AppCompatDelegate.MODE_NIGHT_YES);
                prefs.edit().putBoolean("IS_NIGHT", true).apply();
            }

            prefs.edit().putBoolean("IS_SYSTEM", false).apply();
            return true;
        }   
    
}

【讨论】:

  • 谢谢你,这很完美,但是当我退出应用程序然后再次启动时,虽然我已经切换了默认系统模式,但它仍然处于活动状态。这里有可能让它以这种方式工作吗?
  • @amtrax 谢谢,请检查更新的答案
猜你喜欢
  • 2022-07-19
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 2021-12-09
  • 1970-01-01
  • 2022-01-26
  • 2022-08-10
  • 1970-01-01
相关资源
最近更新 更多