在Application中初始化Colorful
public class SampleApp extends Application { @Override public void onCreate() { super.onCreate(); Colorful.init(this); } }
如果你想设置默认的主题色,可以使用defaults()
public class SampleApp extends Application { @Override public void onCreate() { super.onCreate(); Colorful.defaults() .primaryColor(Colorful.ThemeColor.RED) .accentColor(Colorful.ThemeColor.BLUE) .translucent(false) .dark(true); Colorful.init(this); } }
当然你也可以随时改变主题,只要调用一下apply()即可:
Colorful.config(this) .primaryColor(Colorful.ThemeColor.RED) .accentColor(Colorful.ThemeColor.BLUE) .translucent(false) .dark(true) .apply();
想使用主题,还得让你的Activity继承CActivity:
public class MainActivity extends CActivity
Colorful内置了一个ColorPickerDialog,可以直接设置主题:
public class MainActivity extends CActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setSupportActionBar(((Toolbar) findViewById(R.id.toolbar))); ColorPickerDialog dialog = new ColorPickerDialog(this); dialog.setOnColorSelectedListener(new ColorPickerDialog.OnColorSelectedListener() { @Override public void onColorSelected(Colorful.ThemeColor color) { //TODO: Do something with the color } }); dialog.show(); } }
Colorful还内置了一个preference可以自动设置主题
用colorpicker:primary_color & colorpicker:accent_color告诉Colorful设置哪个颜色。
github
https://github.com/garretyoder/Colorful