LVGL V8之Extending the current theme

133 篇文章 110 订阅

创建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运行效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值