我在运行时有一个用于更改应用程序主题的设置屏幕.我知道如何创建材料设计主题.我已经在我的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中创建多个主题以将其更改为运行时.
@H_301_12@解决方法:你看过这个演示吗?
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 @R_824_4404@ed. * 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-运行时更改Android素材主题全部内容,希望文章能够帮你解决java-运行时更改Android素材主题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)