static lv_style_t style_btn;
/*Will be called when the styles of the base theme are already added
to add new styles*/
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 and the style apply 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);
}
/**
* Extending the current theme
*/
void lv_100ask_demo_course_2_1_1(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");
}
12 lvgl 主题样式
最新推荐文章于 2024-04-09 19:39:31 发布
文章展示了如何在LVGL图形库中扩展一个现有主题,创建一个新的风格应用于按钮对象,改变背景颜色、边框颜色和宽度。通过`new_theme_apply_cb`回调函数,当基础主题的样式已添加后,新样式被添加到按钮上。此外,还创建了两个具有不同主题的按钮进行对比展示。
摘要由CSDN通过智能技术生成