How to remove child margin in layout

Description

I am trying to use the layout functionality to automatically place objects.

What MCU/Processor/Board and compiler are you using?

visual studio 2017, pc simulator

What LVGL version are you using?

7.1

What do you want to achieve?

A 2x2 grid with no spaces between children.

What have you tried so far?

I created a container that has 0 inner padding which seems to work correctly but it seems like the children objects have a margin property that I cannot change. I used lv_obj_set_style_local_margin_all to set all margins to 0 but still there is space between the children and the parent layout. The children are sized to be half of the width and height of the screen so I should get a 2x2 grid. Is there a way to remove these spaces between child and parent?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

	lv_obj_t * container;
	container = lv_cont_create(lv_scr_act(), NULL);
	lv_cont_set_fit(container, LV_FIT_NONE);
	lv_cont_set_layout(container, LV_LAYOUT_GRID);
	lv_coord_t screen_width = lv_obj_get_width(lv_scr_act());
	lv_coord_t screen_height = lv_obj_get_height(lv_scr_act());
	lv_obj_set_size(container, screen_width, screen_height);
	lv_obj_set_style_local_pad_inner(container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);

	lv_obj_t * rect;
	for (int x = 0; x < 4; x++) {
		rect=lv_obj_create(container, NULL);
		lv_obj_set_style_local_margin_all(rect, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
		lv_obj_set_size(rect, screen_width / 2, screen_height / 2);
	}

Screenshot and/or video

Hi @wzheng1,

You just need to remove all padding in the large container:

	lv_obj_t * container;
	container = lv_cont_create(lv_scr_act(), NULL);
	lv_cont_set_fit(container, LV_FIT_NONE);
	lv_cont_set_layout(container, LV_LAYOUT_GRID);
	lv_coord_t screen_width = lv_obj_get_width(lv_scr_act());
	lv_coord_t screen_height = lv_obj_get_height(lv_scr_act());
	lv_obj_set_size(container, screen_width, screen_height);
	lv_obj_set_style_local_pad_all(container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
	lv_obj_set_style_local_pad_inner(container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);


	lv_obj_t * rect;
	for (int x = 0; x < 4; x++) {
		rect=lv_obj_create(container, NULL);
		lv_obj_set_size(rect, screen_width / 2, screen_height / 2);
	}


Kind Regards,

Pete

Hi @pete-pjb,

It works.

Thanks,

Walter

1 Like