第一步:
添加libs
compile 'com.android.support:recyclerview-v7:25.3.1'
//主题框架
compile 'com.bilibili:magicasakura:0.1.5@aar'
需要添加recycleview否则会报错。
res文件中添加color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="theme_color_primary">#fb7299</color>
<color name="theme_color_primary_dark">#b85671</color>
<color name="theme_color_primary_trans">#99f0486c</color>
<color name="theme_color_secondary">#fb7299</color>
<color name="text_primary_color">#2d2d2d</color>
<color name="window_background">#eaeaea</color>
<color name="black_light">#333333</color>
<color name="white">#ffffff</color>
<color name="white_trans">#99ffffff</color>
<color name="gray_dark">#999999</color>
<color name="gray_trans">#44999999</color>
<color name="gray_light">#dcdcdc</color>
<!-- theme color -->
<color name="pink">#fb7299</color>
<color name="pink_dark">#b85671</color>
<color name="pink_trans">#99f0486c</color>
<color name="blue">#2196F3</color>
<color name="blue_dark">#1565C0</color>
<color name="blue_trans">#B41A78C3</color>
<color name="purple">#673AB7</color>
<color name="purple_dark">#311B92</color>
<color name="purple_trans">#99673AB7</color>
<color name="green">#4CAF50</color>
<color name="green_dark">#2E7D32</color>
<color name="green_trans">#994CAF50</color>
<color name="green_light">#8BC34A</color>
<color name="green_light_dark">#558B2F</color>
<color name="green_light_trans">#998BC34A</color>
<color name="yellow">#FDD835</color>
<color name="yellow_dark">#FBC02D</color>
<color name="yellow_trans">#99FDD835</color>
<color name="orange">#FF9800</color>
<color name="orange_dark">#EF6C00</color>
<color name="orange_trans">#99FF9800</color>
<color name="red">#F44336</color>
<color name="red_dark">#C62828</color>
<color name="red_trans">#99F44336</color>
</resources>
其中
theme_color_primary
是最为重要的在bilibili封装的组件中设置需要设置的主题为这个颜色,然后调用代码就可以更改主题。
style中设置:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowNoTitle">true</item>
可以选择不这样设置用组件中封装的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实现这个方法
implements ThemeUtils.switchColor
主题代码设置放在
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