java-运行时更改Android素材主题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-运行时更改Android素材主题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3879字,纯文字阅读大概需要6分钟。
内容图文
![java-运行时更改Android素材主题](/upload/InfoBanner/zyjiaocheng/667/0d35671d6769484c92a39740fb4ff840.jpg)
我在运行时有一个用于更改应用程序主题的设置屏幕.我知道如何创建材料设计主题.我已经在我的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中创建多个主题以将其更改为运行时.
解决方法:
你看过这个演示吗?
看到这个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-运行时更改Android素材主题全部内容,希望文章能够帮你解决java-运行时更改Android素材主题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。