创建theme
- 定义style变量
static lv_style_t style_btn;
- theme回调处理
static void new_theme_apply_cb(lv_theme_t* th, lv_obj_t* obj)
{
LV_UNUSED(th);
if (lv_obj_check_type(obj, &lv_btn_class)) //检查是否为Button class
{
lv_obj_add_style(obj, &style_btn, 0); //添加style
}
}
- 初时化style,设置背景颜色,边框颜色,边框宽度
lv_style_init(&style_btn);
lv_style_set_bg_color(&style_btn, lv_palette_main(LV_PALETTE_GREEN));
lv_style_set_border_color(&style_btn, lv_palette_darken(LV_PALETTE_GREEN, 3));
lv_style_set_border_width(&style_btn, 3);
- 获取显示主题
lv_theme_t* th_act = lv_disp_get_theme(NULL);
- 保存显示主题到th_new变量
static lv_theme_t th_new;
th_new = *th_act;
- 设置parent对象的主题
lv_theme_set_parent(&th_new, th_act);
- 设置主题回调处理函数
lv_theme_set_apply_cb(&th_new, new_theme_apply_cb);
- 指定新的主题到当前显示
lv_disp_set_theme(NULL, &th_new);
- 创建旧主题Button
lv_obj_t* btn;
lv_obj_t* label;
btn = lv_btn_create(lv_scr_act());
lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, 20);
label = lv_label_create(btn);
lv_label_set_text(label, "Original theme");
- 配置新主题后创建Button
new_theme_init_and_set();
btn = lv_btn_create(lv_scr_act());
lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, -20);
label = lv_label_create(btn);
lv_label_set_text(label, "New theme");
完整代码,仅供参考
static lv_style_t style_btn;
static void new_theme_apply_cb(lv_theme_t* th, lv_obj_t* obj)
{
LV_UNUSED(th);
if (lv_obj_check_type(obj, &lv_btn_class)) {
lv_obj_add_style(obj, &style_btn, 0);
}
}
static void new_theme_init_and_set(void)
{
/*Initialize the styles*/
lv_style_init(&style_btn);
lv_style_set_bg_color(&style_btn, lv_palette_main(LV_PALETTE_GREEN));
lv_style_set_border_color(&style_btn, lv_palette_darken(LV_PALETTE_GREEN, 3));
lv_style_set_border_width(&style_btn, 3);
/*Initialize the new theme from the current theme*/
lv_theme_t* th_act = lv_disp_get_theme(NULL);
static lv_theme_t th_new;
th_new = *th_act;
/*Set the parent theme ans the style applay callback for the new theme*/
lv_theme_set_parent(&th_new, th_act);
lv_theme_set_apply_cb(&th_new, new_theme_apply_cb);
/*Assign the new theme the the current display*/
lv_disp_set_theme(NULL, &th_new);
}
static void lv_example_style_14(void)
{
lv_obj_t* btn;
lv_obj_t* label;
btn = lv_btn_create(lv_scr_act());
lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, 20);
label = lv_label_create(btn);
lv_label_set_text(label, "Original theme");
new_theme_init_and_set();
btn = lv_btn_create(lv_scr_act());
lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, -20);
label = lv_label_create(btn);
lv_label_set_text(label, "New theme");
}
调用lv_example_style_14运行效果