第一步:
添加libs
compile 'com.android.support:recyclerview-v7:25.3.1'
//主题框架
compile 'com.bilibili:magicasakura:0.1.5@aar'
需要添加recycleview否则会报错。
res文件中添加color.xml
#3F51B5
#303F9F
#FF4081
#fb7299
#b85671
#99f0486c
#fb7299
#2d2d2d
#eaeaea
#333333
#ffffff
#99ffffff
#999999
#44999999
#dcdcdc
#fb7299
#b85671
#99f0486c
#2196F3
#1565C0
#B41A78C3
#673AB7
#311B92
#99673AB7
#4CAF50
#2E7D32
#994CAF50
#8BC34A
#558B2F
#998BC34A
#FDD835
#FBC02D
#99FDD835
#FF9800
#EF6C00
#99FF9800
#F44336
#C62828
#99F44336
其中
theme_color_primary
是最为重要的在bilibili封装的组件中设置需要设置的主题为这个颜色,然后调用代码就可以更改主题。
style中设置:
可以选择不这样设置用组件中封装的toolbar
第二步设置代码:
appliction中设置:
@Override
public int replaceColorById(Context context, @ColorRes int colorId) {
if (ThemeHelper.isDefaultTheme(context)) {
return context.getResources().getColor(colorId);
}
String theme = getTheme(context);
if (theme != null) {
colorId = getThemeColorId(context, colorId, theme);
}
return context.getResources().getColor(colorId);
}
@Override
public int replaceColor(Context context, @ColorInt int originColor) {
if (ThemeHelper.isDefaultTheme(context)) {
return originColor;
}
String theme = getTheme(context);
int colorId = -1;
if (theme != null) {
colorId = getThemeColor(context, originColor, theme);
}
return colorId != -1 ? getResources().getColor(colorId) : originColor;
}
private String getTheme(Context context) {
if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_STORM) {
return "blue";
} else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_HOPE) {
return "purple";
} else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_WOOD) {
return "green";
} else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_LIGHT) {
return "green_light";
} else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_THUNDER) {
return "yellow";
} else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_SAND) {
return "orange";
} else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_FIREY) {
return "red";
}
return null;
}
private
@ColorRes
int getThemeColorId(Context context, int colorId, String theme) {
switch (colorId) {
case R.color.theme_color_primary:
return context.getResources().getIdentifier(theme, "color", getPackageName());
case R.color.theme_color_primary_dark:
return context.getResources().getIdentifier(theme + "_dark", "color", getPackageName());
case R.color.theme_color_primary_trans:
return context.getResources().getIdentifier(theme + "_trans", "color", getPackageName());
}
return colorId;
}
private
@ColorRes
int getThemeColor(Context context, int color, String theme) {
switch (color) {
case 0xfffb7299:
return context.getResources().getIdentifier(theme, "color", getPackageName());
case 0xffb85671:
return context.getResources().getIdentifier(theme + "_dark", "color", getPackageName());
case 0x99f0486c:
return context.getResources().getIdentifier(theme + "_trans", "color", getPackageName());
}
return -1;
}
application实现这个方法
主题代码设置放在
SharePreference
private static final String CURRENT_THEME = "theme_current";
public static final int CARD_SAKURA = 0x1;
public static final int CARD_HOPE = 0x2;
public static final int CARD_STORM = 0x3;
public static final int CARD_WOOD = 0x4;
public static final int CARD_LIGHT = 0x5;
public static final int CARD_THUNDER = 0x6;
public static final int CARD_SAND = 0x7;
public static final int CARD_FIREY = 0x8;
public static SharedPreferences getSharePreference(Context context) {
return context.getSharedPreferences("multiple_theme", Context.MODE_PRIVATE);
}
public static void setTheme(Context context, int themeId) {
getSharePreference(context).edit()
.putInt(CURRENT_THEME, themeId)
.commit();
}
public static int getTheme(Context context) {
return getSharePreference(context).getInt(CURRENT_THEME, CARD_SAKURA);
}
public static boolean isDefaultTheme(Context context) {
return getTheme(context) == CARD_SAKURA;
}
public static String getName(int currentTheme) {
switch (currentTheme) {
case CARD_SAKURA:
return "THE SAKURA";
case CARD_STORM:
return "THE STORM";
case CARD_WOOD:
return "THE WOOD";
case CARD_LIGHT:
return "THE LIGHT";
case CARD_HOPE:
return "THE HOPE";
case CARD_THUNDER:
return "THE THUNDER";
case CARD_SAND:
return "THE SAND";
case CARD_FIREY:
return "THE FIREY";
}
return "THE RETURN";
}
ThemeUtils.refreshUI(MainActivity.this, new ThemeUtils.ExtraRefreshable() {
@Override
public void refreshGlobal(Activity activity) {
//for global setting, just do once
if (Build.VERSION.SDK_INT >= 21) {
final MainActivity context = MainActivity.this;
ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(null, null, ThemeUtils.getThemeAttrColor(context, android.R.attr.colorPrimary));
setTaskDescription(taskDescription);
getWindow().setStatusBarColor(ThemeUtils.getColorById(context, R.color.theme_color_primary));
}
}
@Override
public void refreshSpecificView(View view) {
}
}
);
https://github.com/Bilibili/MagicaSakura
重要的是设置颜色时使用theme_color_primary