定义及声明
enum {
LV_MENU_ITEM_BUILDER_VARIANT_1,
LV_MENU_ITEM_BUILDER_VARIANT_2
};
typedef uint8_t lv_menu_builder_variant_t;
static void back_event_handler(lv_event_t* e);
static void switch_handler(lv_event_t* e);
lv_obj_t* root_page;
static lv_obj_t* create_text(lv_obj_t* parent, const char* icon, const char* txt,
lv_menu_builder_variant_t builder_variant);
static lv_obj_t* create_slider(lv_obj_t* parent,
const char* icon, const char* txt, int32_t min,int32_t max, int32_t val);
static lv_obj_t* create_switch(lv_obj_t* parent,
const char* icon, const char* txt, bool chk);
返回键事件处理
static void back_event_handler(lv_event_t* e)
{
lv_obj_t* obj = lv_event_get_target(e); // 获取产生事件的对象
lv_obj_t* menu = lv_event_get_user_data(e); // 获取用户数据
if (lv_menu_back_btn_is_root(menu, obj)) { // 根菜单的返回键
lv_obj_t* mbox1 = lv_msgbox_create(NULL, "Hello", "Root back btn click.", NULL, true); //创建消息对话框
lv_obj_center(mbox1); // 居中显示
}
}
switch键事件处理
static void switch_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e); //获取对象产生的事件码
lv_obj_t* menu = lv_event_get_user_data(e); // 获取用户数据
lv_obj_t* obj = lv_event_get_target(e); // 获取产生事件的对象
if (code == LV_EVENT_VALUE_CHANGED) { // LV_EVENT_VALUE_CHANGED事件处理
if (lv_obj_has_state(obj, LV_STATE_CHECKED)) { // 选中处理
lv_menu_set_page(menu, NULL); // 清除主菜单界面显示
lv_menu_set_sidebar_page(menu, root_page);// Set menu page to display in sidebar
lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED, NULL);
}
else { // 未选中处理
lv_menu_set_sidebar_page(menu, NULL); //取消菜单sidebar显示
lv_menu_clear_history(menu); /* Clear history because we will be showing␣
,→the root page later */
lv_menu_set_page(menu, root_page); // 设置菜单主界面
}
}
}
创建文本
static lv_obj_t* create_text(lv_obj_t* parent, const char* icon, const char* txt,
lv_menu_builder_variant_t builder_variant)
{
lv_obj_t* obj = lv_menu_cont_create(parent); // 创建菜单cont容器对象
lv_obj_t* img = NULL;
lv_obj_t* label = NULL;
if (icon) {
img = lv_img_create(obj); // 创建图片对象
lv_img_set_src(img, icon); // 设置图片显示资源
}
if (txt) {
label = lv_label_create(obj); //创建label
lv_label_set_text(label, txt); //设置label显示内容
lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); //循环滚动模式
lv_obj_set_flex_grow(label, 1);
}
if (builder_variant == LV_MENU_ITEM_BUILDER_VARIANT_2 && icon && txt) {
lv_obj_add_flag(img, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK); // 添加LV_OBJ_FLAG_FLEX_IN_NEW_TRACK标志
lv_obj_swap(img, label); // Swap the positions of two objects
}
return obj;
}
创建slider
static lv_obj_t* create_slider(lv_obj_t* parent, const char* icon, const char* txt, int32_t min, int32_t max,
int32_t val)
{
lv_obj_t* obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_2); // 创建自定义text
lv_obj_t* slider = lv_slider_create(obj); // 创建slider对象
lv_obj_set_flex_grow(slider, 1);
lv_slider_set_range(slider, min, max); // Sets the width or height (on main axis) to grow the object in order fill the free space
lv_slider_set_value(slider, val, LV_ANIM_OFF); // 设置slider的值
if (icon == NULL) {
lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK); // 添加LV_OBJ_FLAG_FLEX_IN_NEW_TRACK标志
}
return obj;
}
创建switch
static lv_obj_t* create_switch(lv_obj_t* parent, const char* icon, const char* txt, bool chk)
{
lv_obj_t* obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_1); // 创建自定义text
lv_obj_t* sw = lv_switch_create(obj); // 创建switch开关
lv_obj_add_state(sw, chk ? LV_STATE_CHECKED : 0); // 添加事件
return obj;
}
Complex Menu
static void lv_example_menu_5(void)
{
lv_obj_t* menu = lv_menu_create(lv_scr_act()); // 创建菜单对象
lv_color_t bg_color = lv_obj_get_style_bg_color(menu, 0); //获取菜单背景色
if (lv_color_brightness(bg_color) > 127) { // 背景颜色亮度大于127
lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 10), 0); //设置背景颜色
}
else {
lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 50), 0);//设置背景颜色
}
lv_menu_set_mode_root_back_btn(menu, LV_MENU_ROOT_BACK_BTN_ENABLED);// 使能根菜单返回键
lv_obj_add_event_cb(menu, back_event_handler, LV_EVENT_CLICKED, menu);//添加返回键点击事件
lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));// 设置大小
lv_obj_center(menu); // 居中显示
lv_obj_t* cont;
lv_obj_t* section;
/*Create sub pages*/
lv_obj_t* sub_mechanics_page = lv_menu_page_create(menu, NULL); //创建菜单界面
lv_obj_set_style_pad_hor(sub_mechanics_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
lv_menu_separator_create(sub_mechanics_page); //menu separator
section = lv_menu_section_create(sub_mechanics_page); // Create a menu section object
create_slider(section, LV_SYMBOL_SETTINGS, "Velocity", 0, 150, 120); // 创建Velocity slider
create_slider(section, LV_SYMBOL_SETTINGS, "Acceleration", 0, 150, 50);// 创建Acceleration slider
create_slider(section, LV_SYMBOL_SETTINGS, "Weight limit", 0, 150, 80); // 创建Weight limit slider
lv_obj_t* sub_sound_page = lv_menu_page_create(menu, NULL); // 创建sub_sound_page菜单界面
lv_obj_set_style_pad_hor(sub_sound_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);//设置水平PAD间距
lv_menu_separator_create(sub_sound_page); //menu separator
section = lv_menu_section_create(sub_sound_page); // Create a menu section object
create_switch(section, LV_SYMBOL_AUDIO, "Sound", false); // 创建Sound switch开关
lv_obj_t* sub_display_page = lv_menu_page_create(menu, NULL); // 创建sub_display_page 菜单界面
lv_obj_set_style_pad_hor(sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
lv_menu_separator_create(sub_display_page); //menu separator
section = lv_menu_section_create(sub_display_page); // Create a menu section object
create_slider(section, LV_SYMBOL_SETTINGS, "Brightness", 0, 150, 100); // 创建 Brightness slider
lv_obj_t* sub_software_info_page = lv_menu_page_create(menu, NULL); // 创建sub_software_info_page 菜单界面
lv_obj_set_style_pad_hor(sub_software_info_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
section = lv_menu_section_create(sub_software_info_page); // Create a menu section object
create_text(section, NULL, "Version 1.0", LV_MENU_ITEM_BUILDER_VARIANT_1);// 创建显示版本文本"Version 1.0"
lv_obj_t* sub_legal_info_page = lv_menu_page_create(menu, NULL); // 创建sub_legal_info_page 菜单界面
lv_obj_set_style_pad_hor(sub_legal_info_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
section = lv_menu_section_create(sub_legal_info_page);// Create a menu section object
for (uint32_t i = 0; i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?