Hi,大家好,欢迎来到树莓派之littlevGL课堂,我们在上节课中学习了容器控件(lv_cont),本节课带大家来学习一下下拉列表控件,相信大家对下拉列表肯定很熟悉了,比如我们想让用户选择几个固定的选项的时候,下拉列表就很合适了!那么废话少说! 一 有图有真相 不过既然有下拉列表,那么肯定就有上拉列表,看图! 其实就是列表内容的展示方向不同而已,当下拉列表的下面还有很多可展示区域时,就可以选择往下弹出选择界面,但是当没有足够的展示区域时,我们可以选择往上弹出选择界面。 二 代码分析 用户可下载"圆弧对象(lv_arc)的使用"文章末尾的Demo工程(点击我直接下载),参考圆弧对象(lv_arc)的使用文章,将以下Demo代码复制到Demo工程test.c文件空白处.然后在test.c文件的create_test()函数中增加 ddlist_test1() 和 ddlist_test2()函数测试。 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); } } 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.上拉 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); lv_obj_set_event_cb(ddlist, ddlist_event_handler); } OK,那么本节课先到这里,那么我们下节课学习仪表控件,我们下节课再见! |