How to use object padding

Description

May I ask you to help me with an object alignment? It seems I miss something important.
I try to use padding to create gaps between objects and between an object and a screen border.

I already tried many combination but the result is still the same - NO GAP. Unfortunately I did not find any example how to use it. I know it is possible to set offset of the alignment but I would like to use padding (or margin) because it can be constant for all objects independently on the alignment type (LEFT, MID, RIGHT)

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

ttgo watch + PlatformIO

What LVGL version are you using?

7.3.1

Code to reproduce

    lv_obj_t * container = lv_obj_create( lv_scr_act(), NULL );
    lv_obj_set_size(container, 240, 240);
    lv_obj_set_style_local_pad_inner( container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 10 );
    lv_obj_align(container, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);

    lv_obj_t *btn1 = lv_btn_create(container, NULL);
    lv_obj_align(btn1, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);

    lv_obj_t *btn2 = lv_btn_create(container, NULL);
    lv_obj_set_style_local_pad_left(btn2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 10 );
    lv_obj_set_style_local_pad_top(btn2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 10 );
    lv_obj_align(btn2, btn1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

Hi
As the documentation in Base object section says:

Margin and padding gets important when Layout or Auto-fit is used by other widgets.

So I think, you have to use a container object instead of a simple object. and use lv_cont_set_layout based on the layout you want.

1 Like

That’s correct. Padding and margin are only used by container-based objects. They are ignored by lv_obj_align.

1 Like

It works with containers. Thanks a lot!

I just wondering why the function have the prefix lv_obj when it doesn’t work for common objects but for containers only.

It’s because style properties can be set on all objects, but not every object uses every style property.

This will be more logical in v8 when we introduce a new layout system that works on all objects.

It sounds great. I’m looking forward for v8. Thanks a lot for you support.