Hello,
I have container with label in it. I need to place text with label in the middle of container. Currently I have text at the top of container. How to move it in the middle of container.

lv_obj_t * create_label_for_container(lv_obj_t * parent, lv_obj_t * label, const char* info)
{
label = lv_label_create(parent, NULL);
lv_obj_align(label, parent, LV_ALIGN_CENTER, 0, 0);
lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label, info);
lv_obj_set_style_local_text_color(parent, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
return label;
}
lv_obj_t * parent
is container.
Is there a layout set on the container?
Function below creates container:
lv_obj_t * create_container(lv_obj_t * screen, lv_obj_t * container)
{
container = lv_cont_create(screen, NULL);
lv_cont_set_layout(container, LV_LAYOUT_PRETTY_MID);
lv_obj_set_drag_parent(container, true);
lv_cont_set_fit2(container, LV_FIT_PARENT, LV_FIT_NONE);
lv_obj_set_height(container, BTN_W * 3);
lv_obj_align(container, screen, LV_ALIGN_IN_TOP_MID, 0, 0);
lv_obj_add_style(container, LV_OBJ_PART_MAIN, &container_style);
return container;
}
Since you are using lv_cont_set_layout(container, LV_LAYOUT_PRETTY_MID)
, calling lv_obj_align
on the label won’t work, as the container positions it itself.
Currently I have code that creates container:
lv_obj_t * container1 = lv_cont_create(test_screen, NULL);
lv_cont_set_fit2(container1, LV_FIT_PARENT, LV_FIT_NONE);
lv_obj_set_height(container1,h);
lv_obj_align(container1, test_screen, LV_ALIGN_IN_TOP_MID, 0, 0);
lv_obj_add_style(container1, LV_OBJ_PART_MAIN, &container_style);
lv_obj_t * label1;
create_label(container1,label1, “AAAAAAAAAAAAAAABBBBBBBBBBBBBBBBB”);
And I have function to create label:
lv_obj_t * create_label(lv_obj_t * parent, lv_obj_t * label, const char* info)
{
label = lv_label_create(parent, NULL);
lv_obj_align(label, parent, LV_ALIGN_CENTER, 0, 0);
lv_label_set_text(label, info);
lv_obj_set_style_local_text_color(parent, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
return label;
}
As result I have text pushed to right:
How to put it in the middle?