android怎样实现主题色彩切换功能
Android实现主题色彩切换功能通常有以下几种方法:
// 切换到红色主题
setTheme(R.style.AppTheme_Red);
recreate();
#FF0000
#0000FF
// 获得对应的色彩值
int colorPrimary = ContextCompat.getColor(this, R.color.red);
int colorPrimaryDark = ContextCompat.getColor(this, R.color.dark_red);
int colorAccent = ContextCompat.getColor(this, R.color.red_accent);
// 修改主题色彩
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(colorPrimaryDark);
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(colorPrimary));
button.setBackgroundColor(colorAccent);
请注意,以上两种方法都需要在切换主题时调用recreate()
方法来重新创建Activity,以使主题切换生效。
TOP