LVGL_基础控件dropdown
1、创建一个控件dropdown
lv_obj_t * dd = lv_dropdown_create(lv_scr_act());
lv_dropdown_set_options(dd, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
2、追加选项
lv_dropdown_add_option(dd, "11", 10);
3、设置默认选中项
lv_dropdown_set_selected(dd, 0);
4、设置列表创建的方向
5、设置按钮显示的字符
lv_dropdown_set_symbol(dd, LV_SYMBOL_CALL);
6、设置控件一直显示的文字信息
lv_dropdown_set_text(dd, "Some text");
7、修改样式
lv_obj_t * dd_list = lv_dropdown_get_list(dd);
lv_obj_set_style_text_font(dd, &lv_font_montserrat_30, 0);
lv_obj_set_style_text_font(dd_list, &lv_font_montserrat_30, 0);
lv_obj_set_style_text_align(dd, LV_TEXT_ALIGN_RIGHT, 0);
lv_obj_set_style_text_align(dd_list, LV_TEXT_ALIGN_CENTER, 0);
8、使用中文来显示
LV_FONT_DECLARE(lv_font_source_han_sans_bold_14);
lv_obj_set_style_text_font(dd, &lv_font_source_han_sans_bold_14, 0);
lv_obj_set_style_text_font(dd_list, &lv_font_source_han_sans_bold_14, 0);
lv_dropdown_set_options(dd, "一\n二\n三\n四\n五\n六\n七\n八\n九\n十");
9、增加事件
lv_obj_t * btn = lv_btn_create(lv_scr_act());
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
lv_obj_align_to(btn, dd, LV_ALIGN_OUT_TOP_MID, 0, -10);
lv_obj_add_event_cb(btn, btn_event_handler, LV_EVENT_VALUE_CHANGED , dd);
static void btn_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *btn = lv_event_get_target(e);
lv_obj_t *dd = lv_event_get_user_data(e);
if(code == LV_EVENT_VALUE_CHANGED) {
if(lv_obj_has_state(btn, LV_STATE_CHECKED))
lv_dropdown_open(dd);
else
lv_dropdown_close(dd);
}
}
static void dd_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *obj = lv_event_get_target(e);
if (code == LV_EVENT_VALUE_CHANGED)
{
LV_LOG_USER("%d", lv_dropdown_get_selected(obj));
char tmp_buf[32];
lv_dropdown_get_selected_str(obj, tmp_buf, sizeof(tmp_buf));
LV_LOG_USER("%s", tmp_buf);
}
else if(code == LV_EVENT_CLICKED)
{
lv_obj_t * dd_list = lv_dropdown_get_list(obj);
if(dd_list != NULL)
{
lv_obj_set_style_bg_color(dd_list, lv_color_hex(0xc43e1c), 0);
lv_obj_set_style_border_color(dd_list, lv_color_hex(0xcc3e1c), LV_PART_SCROLLBAR);
lv_obj_set_style_bg_color(dd_list, lv_color_hex(0xafbeac), LV_PART_SELECTED);
}
}
}