LVGL lv_cont 容器(8)

lv_cont

lv_cont作用是用来解决变化的子对象布局问题,比如父对象中的子对象增加或者减少时原先的布局为了美观其他子对象位置可能需要改变,手动调用lv_set_pos比较繁琐。所以lv_cont主要有Layout和Auto fit 2个特性来解决这个问题。
相关API在lv_cont.h

layout 设置类型

/** Container layout options*/
enum {
    LV_LAYOUT_OFF = 0, /**< No layout */
    LV_LAYOUT_CENTER, /**< Center objects */

    /**
     * COLUMN:
     * - Place the object below each other
     * - Keep `pad_top` space on the top
     * - Keep `pad_inner` space between the objects
     */
    LV_LAYOUT_COLUMN_LEFT,      /**< Column left align*/
    LV_LAYOUT_COLUMN_MID,       /**< Column middle align*/
    LV_LAYOUT_COLUMN_RIGHT,     /**< Column right align*/

    /**
     * ROW:
     * - Place the object next to each other
     * - Keep `pad_left` space on the left
     * - Keep `pad_inner` space between the objects
     * - If the object which applies the layout has `base_dir == LV_BIDI_DIR_RTL`
     *   the row will start from the right applying `pad.right` space
     */
    LV_LAYOUT_ROW_TOP,          /**< Row top align*/
    LV_LAYOUT_ROW_MID,          /**< Row middle align*/
    LV_LAYOUT_ROW_BOTTOM,       /**< Row bottom align*/


    /**
     * PRETTY:
     * - Place the object next to each other
     * - If there is no more space start a new row
     * - Respect `pad_left` and `pad_right` when determining the available space in a row
     * - Keep `pad_inner` space between the objects in the same row
     * - Keep `pad_inner` space between the objects in rows
     * - Divide the remaining horizontal space equally
     */
    LV_LAYOUT_PRETTY_TOP,       /**< Row top align*/
    LV_LAYOUT_PRETTY_MID,       /**< Row middle align*/
    LV_LAYOUT_PRETTY_BOTTOM,    /**< Row bottom align*/

    /**
     * GRID
     * - Place the object next to each other
     * - If there is no more space start a new row
     * - Respect `pad_left` and `pad_right` when determining the available space in a row
     * - Keep `pad_inner` space between the objects in the same row
     * - Keep `pad_inner` space between the objects in rows
     * - Unlike `PRETTY`, `GRID` always keep `pad_inner` space horizontally between objects
     *   so it doesn't divide the remaining horizontal space equally
     */
    LV_LAYOUT_GRID,   /**< Align same-sized object into a grid*/

    _LV_LAYOUT_LAST
};
typedef uint8_t lv_layout_t;

fit 设置类型

/**
 * How to resize the container around the children.
 */
enum {
    LV_FIT_NONE,  /**< Do not change the size automatically*/
    LV_FIT_TIGHT, /**< Shrink wrap around the children */
    LV_FIT_PARENT, /**< Align the size to the parent's edge*/
    LV_FIT_MAX,  /**< Align the size to the parent's edge first but if there is an object out of it
                     then get larger */
    _LV_FIT_LAST
};
typedef uint8_t lv_fit_t;

样式设置

/*Part of the container*/
enum {
    LV_CONT_PART_MAIN = LV_OBJ_PART_MAIN,
    _LV_CONT_PART_VIRTUAL_LAST = _LV_OBJ_PART_VIRTUAL_LAST,
    _LV_CONT_PART_REAL_LAST = _LV_OBJ_PART_REAL_LAST,
};

可以看出其实是当作基础对象了

例子

void lv_ex_cont_1(void)
{
    lv_obj_t * cont;

    cont = lv_cont_create(lv_scr_act(), NULL);
    lv_obj_set_auto_realign(cont, true);                    /* cont 对象相对屏幕对象自动对齐*/
    lv_obj_align_origo(cont, NULL, LV_ALIGN_CENTER, 0, 0);  /*cont相对屏幕居中*/
	/*这里设置cont 居中并自动对齐是因为下面cont 4个方向都是自动包裹fit模式
	或者左边和上面设置成LV_FIT_NONE禁止自适应*/
    lv_cont_set_fit(cont, LV_FIT_TIGHT);//4 个方向都设置成 LV_FIT_TIGHT,内部调用lv_cont_set_fit4
    lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_MID);//设置成COLUMN_MID layout方式

	lv_obj_t * label;
	label = lv_label_create(cont, NULL);
	lv_label_set_text(label, "Short text");

	/*Refresh and pause here for a while to see how `fit` works*/
	uint32_t t;
	lv_refr_now(NULL);
	t = lv_tick_get();
	while (lv_tick_elaps(t) < 500);

	label = lv_label_create(cont, NULL);
	lv_label_set_text(label, "It is a long text");

	/*Wait here too*/
	lv_refr_now(NULL);
	t = lv_tick_get();
	while (lv_tick_elaps(t) < 500);

	label = lv_label_create(cont, NULL);
	lv_label_set_text(label, "Here is an even longer text");
}

效果
LVGL lv_cont 容器(8)

上一篇:ext4和xfs文件系统的扩容和收缩


下一篇:Red Hat 6扩容超过16T,导致磁盘无法正常识别