Hi,大家好,欢迎来到STM32之littlevGL课堂,我们在上节课中学习了容器控件(lv_cont),本节课带大家来学习一下下拉列表控件,相信大家对下拉列表肯定很熟悉了,比如我们想让用户选择几个固定的选项的时候,下拉列表就很合适了!那么废话少说! 一 有图有真相 这里给大家展示一下下拉列表和上拉列表,其实就是列表内容的展示方向不同而已,当下拉列表的下面还有很多可展示区域时,就可以选择往下弹出选择界面,但是当没有足够的展示区域时,我们可以选择往上弹出选择界面。 二 代码分析 1. 下拉 static void ddlist_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) { char buf[32]; lv_ddlist_get_selected_str(obj, buf, sizeof(buf)); printf("Option: %s\n", buf); } } static void ddlist_test1(void) { /*Create a drop down list*/ lv_obj_t * ddlist = lv_ddlist_create(lv_scr_act(), NULL); lv_ddlist_set_options(ddlist, "Apple\n" "Banana\n" "Orange\n" "Melon\n" "Grape\n" "Raspberry"); lv_ddlist_set_fix_width(ddlist, 150); lv_ddlist_set_draw_arrow(ddlist, true); lv_obj_align(ddlist, NULL, LV_ALIGN_IN_TOP_MID, 0, 20); lv_obj_set_event_cb(ddlist, ddlist_event_handler); } 2.上拉 static void ddlist_test2(void) { /*Create a drop down list*/ lv_obj_t * ddlist = lv_ddlist_create(lv_scr_act(), NULL); lv_ddlist_set_options(ddlist, "Apple\n" "Banana\n" "Orange\n" "Melon\n" "Grape\n" "Raspberry"); lv_ddlist_set_fix_width(ddlist, 150); lv_ddlist_set_fix_height(ddlist, 150); lv_ddlist_set_draw_arrow(ddlist, true); /* Enable auto-realign when the size changes. * It will keep the bottom of the ddlist fixed*/ lv_obj_set_auto_realign(ddlist, true); /*It will be called automatically when the size changes*/ lv_obj_align(ddlist, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -20); } OK,那么本节课先到这里,那么我们下节课学习仪表控件,我们下节课再见! |