gpt4 book ai didi

java - Runtime Change Android 素材主题

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:10 29 4
gpt4 key购买 nike

我有一个用于在运行时更改应用主题的设置屏幕。我知道如何创建 Material 设计主题。我在我的 style.xml 文件中创建了一个

这是我的style.xml的代码:

 <style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primaryBackground</item>
<item name="colorPrimaryDark">@color/primaryBackground</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorControlNormal">@color/primaryBackground</item>
<item name="android:colorControlActivated">@color/primaryBackground</item>
<item name="android:colorControlHighlight">@color/primaryBackground</item>
<item name="android:textColorPrimary">@color/primaryBackground</item>
<item name="android:textColorSecondary">@color/primaryBackground</item>
<item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
<item name="android:textCursorDrawable">@drawable/cursor_indicator</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

现在我想将运行时应用程序主题从绿色更改为紫色或黄色。任何人都可以告诉我如何从主题选择中创建颜色选择器,以及如何在我的 style.xml 中创建多个主题以将其更改为运行时。

最佳答案

你看过这个演示吗?

MultipleThemeMaterialDesign

查看此 class :

public class Preferences {

private static final BoolToStringPref[] PREF_MIGRATION = new BoolToStringPref[]{
new BoolToStringPref(R.string.pref_dark_theme, false,
R.string.pref_theme, R.string.pref_theme_value_red),
};

public static void sync(PreferenceManager preferenceManager) {
Map<String, ?> map = preferenceManager.getSharedPreferences().getAll();
for (String key : map.keySet()) {
sync(preferenceManager, key);
}
}

public static void sync(PreferenceManager preferenceManager, String key) {
Preference pref = preferenceManager.findPreference(key);
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry());
}
}

/**
* Migrate from boolean preferences to string preferences. Should be called only once
* when application is relaunched.
* If boolean preference has been set before, and value is not default, migrate to the new
* corresponding string value
* If boolean preference has been set before, but value is default, simply remove it
* @param context application context
* TODO remove once all users migrated
*/
public static void migrate(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
for (BoolToStringPref pref : PREF_MIGRATION) {
if (pref.isChanged(context, sp)) {
editor.putString(context.getString(pref.newKey), context.getString(pref.newValue));
}

if (pref.hasOldValue(context, sp)) {
editor.remove(context.getString(pref.oldKey));
}
}

editor.apply();
}

public static void applyTheme(ContextThemeWrapper contextThemeWrapper) {
if (Preferences.darkThemeEnabled(contextThemeWrapper)) {
contextThemeWrapper.setTheme(R.style.AppTheme_Blue);
}
}

private static boolean darkThemeEnabled(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getString(context.getString(R.string.pref_theme),
context.getString(R.string.pref_theme_value_red))
.equals(context.getString(R.string.pref_theme_value_blue));
}

private static class BoolToStringPref {
private final int oldKey;
private final boolean oldDefault;
private final int newKey;
private final int newValue;

private BoolToStringPref(@StringRes int oldKey, boolean oldDefault,
@StringRes int newKey, @StringRes int newValue) {
this.oldKey = oldKey;
this.oldDefault = oldDefault;
this.newKey = newKey;
this.newValue = newValue;
}

private boolean isChanged(Context context, SharedPreferences sp) {
return hasOldValue(context, sp) &&
sp.getBoolean(context.getString(oldKey), oldDefault) != oldDefault;
}

private boolean hasOldValue(Context context, SharedPreferences sp) {
return sp.contains(context.getString(oldKey));
}
}
}

查看该演示,它将帮助您了解更多信息。

关于java - Runtime Change Android 素材主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41800164/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com