一.标签部件
1.如何创建标签部件以及设置文本
![2024-01-28T09:54:08.png][3]
void my_lvgl(void)
{lv_obj_t *lable=lv_label_create(lv_scr_act()); //创建一个标签lv_label_set_text(lable,"hello"); //普通更改文字lv_label_set_text_fmt(lable,"saeaw %d",90); //格式化更改文字lv_obj_set_style_bg_opa(lable,200,LV_STATE_DEFAULT); //设置透明度,默认触发lv_obj_set_style_bg_color(lable,lv_color_hex(0xf7b37b),LV_STATE_DEFAULT); //设置背景颜色,主题,默认触发lv_obj_set_style_text_font(lable,&lv_font_montserrat_30,LV_STATE_DEFAULT); //设置字体,lv_config328行lv_obj_set_style_text_color(lable,lv_color_hex(0x12ed56),LV_STATE_DEFAULT); //设置文字颜色//设置个别文字颜色lv_label_set_recolor(lable,true); //开启着色功能lv_label_set_text(lable,"hello #ff0000 lvgl#"); //为lvgl设置颜色}
void my_lvgl(void)
{lv_obj_t *lable=lv_label_create(lv_scr_act());lv_label_set_text(lable,"hello hello hello hello rettreteretr hello hello hello hello hello\n");lv_obj_set_size(lable,100,50);lv_label_set_long_mode(lable,LV_LABEL_LONG_SCROLL) ; //选择长模式,来回滚动lv_label_set_long_mode(lable,LV_LABEL_LONG_SCROLL_CIRCULAR) ; //选择长模式,循环滚动}
二.按钮部件
static void myEvent(lv_event_t* e)
{lv_event_code_t code = lv_event_get_code(e); //获取事件类型if(code== LV_EVENT_VALUE_CHANGED){printf("LV_EVENT_VALUE_CHANGED\n");}return;
}void my_lvgl(void)
{lv_obj_t *btn=lv_btn_create(lv_scr_act()); //创建按钮lv_obj_set_size(btn,100,50); //设置大小lv_obj_set_align(btn,LV_ALIGN_CENTER);//lv_obj_set_style_bg_color(btn,lv_color_hex(0xde4d3e),LV_STATE_PRESSED); //设置背景颜色,按下触发、lv_obj_set_style_bg_color(btn,lv_color_hex(0xff0000),LV_STATE_DEFAULT); //设置背景颜色,默认触发、lv_obj_add_flag(btn,LV_OBJ_FLAG_CHECKABLE); //状态转换lv_obj_add_event_cb(btn,myEvent,LV_EVENT_VALUE_CHANGED,NULL); //状态切换时产生事件,必须要开启}
三.开关部件
lv_obj_t *switch1;
static void myEvent(lv_event_t* e)
{lv_event_code_t code=lv_event_get_code(e);if(code==LV_EVENT_VALUE_CHANGED){//获取开关状态,开1关0if(lv_obj_has_state(switch1,LV_STATE_CHECKED)){printf("LED ON\n");}else{printf("LED OFF\n");}}return;
}void my_lvgl(void)
{switch1=lv_switch_create(lv_scr_act()); //创建开关lv_obj_set_style_bg_color(switch1,lv_color_hex(0xff0000),LV_STATE_DEFAULT|LV_PART_KNOB); //设置手柄默认颜色lv_obj_set_style_bg_color(switch1,lv_color_hex(0xff0000),LV_STATE_CHECKED|LV_PART_INDICATOR); //设置指示器颜色lv_obj_add_state(switch1,LV_STATE_CHECKED); //打开开关,LV_STATE_DISABLED不可修改lv_obj_clear_state(switch1,LV_STATE_CHECKED); //关闭开关bool flage=lv_obj_has_state(switch1,LV_STATE_CHECKED); //获取开关状态,开1关0lv_obj_add_event_cb(switch1,myEvent,LV_EVENT_VALUE_CHANGED,NULL); //当开关状态发生改变时,触发事件
}