从暗模式切换到光模式再返回

时间:2019-09-12 12:44:41

标签: java android android-darkmode

我想将我的应用程序从暗模式切换到亮模式。 我在右上角的选项下拉菜单中实现了2个按钮。 1.暗模式2.亮模式。 当我单击“暗模式”按钮时,主题从浅变为暗,但是如果我单击“亮模式”按钮,则什么也没有发生。

样式XML文件:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

代码:


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.darkmode:
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    break;
            case R.id.lightmode:
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    break;
        }
        return super.onOptionsItemSelected(item);

    }

0 个答案:

没有答案
相关问题