LVGL_基础控件滚轮roller
1、创建滚轮roller控件
lv_obj_t * roller = lv_roller_create ( lv_scr_act ( ) ) ;
lv_roller_set_options ( roller, "1\n" "2\n" "3\n" "4\n" "5\n" "6\n" "7\n" "8\n" "9\n" "10" , LV_ROLLER_MODE_NORMAL) ;
2、创建为无限滚动的模式
lv_roller_set_options ( roller, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" , LV_ROLLER_MODE_INFINITE) ;
3、设置默认选中项(注意:选中项是从0开始的)
lv_roller_set_selected ( roller, 3 , LV_ANIM_ON) ;
4、设置显示的行数(5行)
lv_roller_set_visible_row_count ( roller, 5 ) ;
5、设置显示的高度
lv_obj_set_height ( roller, 200 ) ;
6、设置滚轮的字体
lv_obj_set_style_text_font ( roller, & lv_font_montserrat_22, LV_PART_MAIN) ;
lv_obj_set_style_text_font ( roller, & lv_font_montserrat_22, LV_PART_SELECTED) ;
7、设置滚轮显示汉字
LV_FONT_DECLARE ( lv_font_source_han_sans_bold_14) ;
lv_obj_set_style_text_font ( roller, & lv_font_source_han_sans_bold_14, LV_PART_MAIN) ;
lv_obj_set_style_text_font ( roller, & lv_font_source_han_sans_bold_14, LV_PART_SELECTED) ;
lv_roller_set_options ( roller, "一\n二\n三\n四\n五\n六\n七\n八\n九\n十" , LV_ROLLER_MODE_NORMAL) ;
8、改变选项中文字的位置
lv_obj_set_style_text_align ( roller, LV_TEXT_ALIGN_RIGHT, 0 ) ;
9、添加事件
lv_obj_add_event_cb ( roller, roller_event_handler, LV_EVENT_ALL, NULL ) ;
lv_obj_t * btn = lv_btn_create ( lv_scr_act ( ) ) ;
lv_obj_align_to ( btn, roller, LV_ALIGN_OUT_TOP_MID, 0 , - 10 ) ;
lv_obj_add_event_cb ( btn, btn_event_handler, LV_EVENT_CLICKED , roller) ;
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 * roller = lv_event_get_user_data ( e) ; if ( code == LV_EVENT_CLICKED) { uint32_t sel_opt = lv_roller_get_selected ( roller) ; uint32_t total_opt = lv_roller_get_option_cnt ( roller) - 1 ; if ( sel_opt == total_opt) sel_opt = 0 ; else sel_opt += 1 ; lv_roller_set_selected ( roller, sel_opt, LV_ANIM_ON) ; lv_event_send ( roller, LV_EVENT_VALUE_CHANGED, 0 ) ; }
}
static void roller_event_handler ( lv_event_t * e)
{ lv_event_code_t code = lv_event_get_code ( e) ; lv_obj_t * roller = lv_event_get_target ( e) ; if ( code == LV_EVENT_VALUE_CHANGED) { LV_LOG_USER ( "%d" , lv_roller_get_selected ( roller) ) ; char tmp_buf[ 8 ] ; lv_roller_get_selected_str ( roller, tmp_buf, sizeof ( tmp_buf) ) ; LV_LOG_USER ( "%s" , tmp_buf) ; }
}
10、设置背景颜色
lv_obj_set_style_bg_color ( roller, lv_color_hex ( 0xd35230 ) , 0 ) ;
11、设置选中项的背景颜色
lv_obj_set_style_bg_color ( roller, lv_color_hex ( 0xF0F0F0 ) , LV_PART_SELECTED) ;
12、设置选项之间的间隔
lv_obj_set_style_text_line_space ( roller, 50 , 0 ) ;