简体   繁体   English

如何让黑暗模式在我的应用程序上运行?

[英]How can I get dark mode to work on my app?

I have set up a ListPreference with different values for the user's dark mode preference.我已经为用户的暗模式首选项设置了一个具有不同值的 ListPreference。 Right now, I just want the app to work with the new dark mode implementation introduced in Android Q. If I set the system dark mode, the app changes because I have enabled isforcedarkallowed in styles.xml .现在,我只是想应用到工作与Android的问:如果我公司推出了新的黑暗模式实现设置系统暗模式,应用程序的变化,因为我已经启用isforcedarkallowedstyles.xml

SettingsFragment:设置片段:

public static class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
        PreferenceScreen screen = getPreferenceScreen();
        final ListPreference listPreference = (ListPreference) findPreference("theme");
        Preference preference = (Preference) findPreference("notq");
        int api = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
        if (api > 28) {
            screen.removePreference(preference);
        }
        if (api < 29) {
            screen.removePreference(listPreference);
        }
        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                listPreference.setValue(newValue.toString());
                theme = String.valueOf(listPreference.getEntry());
                Log.d("debug", theme);
                if (theme.equals("Light")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
                }
                if (theme.equals("Dark")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
                }
                if (theme.equals("System default")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
                }
                return true;
            }
        });
    }
}

Arrays.xml (values for dark mode preference) Arrays.xml(暗模式首选项的值)

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Reply Preference -->
<string-array name="theme_entries">
    <item>Light</item>
    <item>Dark</item>
    <item>System default</item>

</string-array>

<string-array name="theme_values">
    <item>light</item>
    <item>dark</item>
    <item>default1</item>
</string-array>
</resources>

My listpreference:我的列表偏好:

 <ListPreference
    android:id="@+id/list"
    app:entries="@array/theme_entries"
    app:entryValues="@array/theme_values"
    app:key="theme"
    app:title="@string/theme_title"
    app:useSimpleSummaryProvider="true" />

And the app theme I'm using (styles.xml):我正在使用的应用程序主题 (styles.xml):

 <style name="AppTheme2" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:forceDarkAllowed" tools:targetApi="q">true</item>
    <item name="android:isLightTheme" tools:targetApi="q">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#FFFFFF</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Any suggestions?有什么建议?

In your styles.xml you should not take Theme.AppCompat.DayNight.DarkActionBar as a parent while you enabled forceDarkAllowed .在您的styles.xml您不应在启用forceDarkAllowed时将Theme.AppCompat.DayNight.DarkActionBar作为父forceDarkAllowed Change that to Theme.AppCompat or Theme.MaterialComponents .将其更改为Theme.AppCompatTheme.MaterialComponents You cannot inherit from a daynight theme while forceDarkAllowed is set to true.forceDarkAllowed设置为 true 时,您不能从forceDarkAllowed主题继承。 You can read about that here https://developer.android.com/guide/topics/ui/look-and-feel/darktheme section forcedark .您可以在此处阅读有关内容https://developer.android.com/guide/topics/ui/look-and-feel/darktheme部分forceark

String theme = String.valueOf(listPreference.getEntry()); 

Should work fine, you just didn't declare theme as a string应该可以正常工作,您只是没有将主题声明为字符串

:) :)

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

相关问题 如何使用开关在我的应用程序上设置暗模式 - How can I set dark Mode on my app with the switch 如何在我的 Android 工作室中正确开发暗模式? - How can I correctly to develop the dark mode in my Android studio? 单击按钮时如何在我的 android 应用程序中添加暗模式 - How to add dark mode in my android app when i click the button 如何在我的 android 应用程序中保存夜间模式的状态? - How can I save the state of the night mode in my android app? 如何使我的cardLayout工作? - How can I get my cardLayout to work? 如何使应用程序具有黑暗和明亮的主题? - How can I make an app dark and light theme? 我不小心删除了 themes.xml(night) 文件。 现在我想要它回来为我的应用程序启用暗模式 - I accidently deleted themes.xml(night) file. now i want it back to enable dark mode to my app 如何在 java swing gui 应用程序中实现明暗模式? - How can i implement dark and light mode in a java swing gui application? 当应用程序首次启动时,我如何在第一个活动上实现暗模式 - How can i implement dark mode on the first activity while the application launched for the first time 如何让 Firebase 电话身份验证适用于我的应用? - How do I get Firebase Phone Authentication to work for my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM