事件处理回调函数
static void event_cb(lv_event_t* e)
{
lv_obj_t* dropdown = lv_event_get_target(e); // 获取产生事件的对象
char buf[64];
lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf)); //获取dropdown对象选择的内容
LV_LOG_USER("'%s' is selected", buf); // 打印选择的内容
}
Create a menu from a drop-down list
/**
* Create a menu from a drop-down list and show some drop-down list features and styling
*/
static void lv_example_dropdown_3(void)
{
/*Create a drop down list*/
lv_obj_t* dropdown = lv_dropdown_create(lv_scr_act()); // 创建dropdown对象
lv_obj_align(dropdown, LV_ALIGN_TOP_LEFT, 10, 10); //LV_ALIGN_TOP_LEFT方式对齐
lv_dropdown_set_options(dropdown, "New project\n"
"New file\n"
"Save\n"
"Save as ...\n"
"Open project\n"
"Recent projects\n"
"Preferences\n"
"Exit"); // 设置选项内容
/*Set a fixed text to display on the button of the drop-down list*/
lv_dropdown_set_text(dropdown, "Menu"); // 设置固定文本Menu
/*Use a custom image as down icon and flip it when the list is opened*/
LV_IMG_DECLARE(img_caret_down) //img_caret_down图片资源声明
lv_dropdown_set_symbol(dropdown, &img_caret_down); // 设置dropdown符号
lv_obj_set_style_transform_angle(dropdown, 1800, LV_PART_INDICATOR | LV_STATE_CHECKED); // LV_PART_INDICATOR 和 LV_STATE_CHECKED属性时图片旋转180度
/*In a menu we don't need to show the last clicked item*/
lv_dropdown_set_selected_highlight(dropdown, false); //选中项关闭高亮显示
lv_obj_add_event_cb(dropdown, event_cb, LV_EVENT_VALUE_CHANGED, NULL);//dropdown添加LV_EVENT_VALUE_CHANGED画件
}
运行效果
lv_dropdown_set_selected_highlight(dropdown, true); // 设成true打开高亮
运行效果