如何在首选项中引用Android主题?
如何在不为每个主题定义常量的情况下在首选项中保存主题 样式资源id的获取和保存非常简单,但也可能在更新中更改如何在首选项中引用Android主题?,android,android-theme,android-resources,android-styles,Android,Android Theme,Android Resources,Android Styles,如何在不为每个主题定义常量的情况下在首选项中保存主题 样式资源id的获取和保存非常简单,但也可能在更新中更改 样式名称似乎是更好的选择。如果是这样做的话,那么如何在给定资源id的情况下获取主题名称,以及如何在给定主题名称的情况下获取资源id?我通过使用主题名称和。完整代码: public static final String PREFERENCE_THEME = "theme"; public static final int[] THEMES = {R.style.Theme_Blue, R
样式名称似乎是更好的选择。如果是这样做的话,那么如何在给定资源id的情况下获取主题名称,以及如何在给定主题名称的情况下获取资源id?我通过使用主题名称和。完整代码:
public static final String PREFERENCE_THEME = "theme";
public static final int[] THEMES = {R.style.Theme_Blue, R.style.Theme_Green, R.style.Theme_Orange, R.style.Theme_Purple, R.style.Theme_Red};
private static final int DEFAULT_THEME = R.style.Theme_Green;
public int getTheme() {
final String themeName = preferences.getString(PREFERENCE_THEME, getThemeName(DEFAULT_THEME));
for (int i = 0; i < THEMES.length; i++) {
int resId = THEMES[i];
String candidateThemeName = this.getThemeName(resId);
if (themeName.equals(candidateThemeName)) {
return resId;
}
}
// The theme in preferences doesn't exist anymore
this.setTheme(DEFAULT_THEME);
return DEFAULT_THEME;
}
public void setTheme(int resId) {
final String themeName = getThemeName(resId);
Editor editor = preferences.edit();
editor.putString(PREFERENCE_THEME, themeName);
editor.commit();
}
private String getThemeName(int resId) {
return context.getResources().getResourceEntryName(resId);
}
publicstaticfinalstringpreference\u THEME=“THEME”;
公共静态最终int[]主题={R.style.Theme_蓝色,R.style.Theme_绿色,R.style.Theme_橙色,R.style.Theme_紫色,R.style.Theme_红色};
private static final int DEFAULT_THEME=R.style.THEME_绿色;
公共int getTheme(){
最后一个字符串themeName=preferences.getString(首选项主题,getThemeName(默认主题));
for(int i=0;i
您可以尝试使用xml解析器解析xml并检索值