[英]How can I get dark mode to work on my app?
我已經為用戶的暗模式首選項設置了一個具有不同值的 ListPreference。 現在,我只是想應用到工作與Android的問:如果我公司推出了新的黑暗模式實現設置系統暗模式,應用程序的變化,因為我已經啟用isforcedarkallowed
在styles.xml
。
設置片段:
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(暗模式首選項的值)
<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>
我的列表偏好:
<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" />
我正在使用的應用程序主題 (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>
有什么建議?
在您的styles.xml
您不應在啟用forceDarkAllowed
時將Theme.AppCompat.DayNight.DarkActionBar
作為父forceDarkAllowed
。 將其更改為Theme.AppCompat
或Theme.MaterialComponents
。 當forceDarkAllowed
設置為 true 時,您不能從forceDarkAllowed
主題繼承。 您可以在此處閱讀有關內容https://developer.android.com/guide/topics/ui/look-and-feel/darktheme部分forceark 。
String theme = String.valueOf(listPreference.getEntry());
應該可以正常工作,您只是沒有將主題聲明為字符串
:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.