基本使用

image.png

  1. // 创建容器container
  2. lv_obj_t* container = lv_cont_create(lv_scr_act(), NULL);
  3. // 设置布局
  4. lv_cont_set_layout(container, LV_LAYOUT_COLUMN_LEFT);
  5. // 设置大小适应
  6. lv_cont_set_fit(container, LV_FIT_PARENT);
  7. // 水平、垂直方向
  8. //lv_cont_set_fit2(container, LV_FIT_PARENT, LV_FIT_PARENT);
  9. // 上下左右方向
  10. //lv_cont_set_fit4(container, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_PARENT);
  11. // 设置背景样式
  12. static lv_style_t style;
  13. lv_style_init(&style);
  14. lv_style_set_radius(&style, LV_STATE_DEFAULT, 0);
  15. lv_style_set_border_color(&style, LV_STATE_DEFAULT, LV_COLOR_RED);
  16. lv_style_set_border_width(&style, LV_STATE_DEFAULT, 2);
  17. // 设置内边距
  18. lv_style_set_pad_all(&style, LV_STATE_DEFAULT, 0);
  19. lv_style_set_pad_top(&style, LV_STATE_DEFAULT, 30);
  20. lv_style_set_pad_left(&style, LV_STATE_DEFAULT, 10);
  21. lv_style_set_pad_right(&style, LV_STATE_DEFAULT, 0);
  22. lv_style_set_pad_bottom(&style, LV_STATE_DEFAULT, 0);
  23. // 设置子控件之间的距离
  24. lv_style_set_pad_inner(&style, LV_STATE_DEFAULT, 0);
  25. // 设置外边距 此处无效
  26. lv_style_set_margin_all(&style, LV_STATE_DEFAULT, 0);
  27. lv_style_set_margin_top(&style, LV_STATE_DEFAULT, 10);
  28. lv_style_set_margin_left(&style, LV_STATE_DEFAULT, 30);
  29. lv_obj_add_style(container, LV_OBJ_PART_MAIN, &style);
  30. // 添加子控件btn1
  31. lv_obj_t* btn1 = lv_btn_create(container, NULL);
  32. lv_obj_t* label1 = lv_label_create(btn1, NULL);
  33. lv_label_set_text(label1, "button1");
  34. static lv_style_t style1;
  35. lv_style_init(&style1);
  36. lv_style_set_radius(&style1, LV_STATE_DEFAULT, 0);
  37. lv_style_set_border_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLUE);
  38. lv_obj_add_style(btn1, LV_OBJ_PART_MAIN, &style1);
  39. // 添加子控件btn2
  40. lv_obj_t* btn2 = lv_btn_create(container, NULL);
  41. lv_obj_t* label2 = lv_label_create(btn2, NULL);
  42. lv_label_set_text(label2, "button2");
  43. static lv_style_t style2;
  44. lv_style_init(&style2);
  45. lv_style_set_radius(&style2, LV_STATE_DEFAULT, 0);
  46. lv_style_set_border_color(&style2, LV_STATE_DEFAULT, LV_COLOR_BLUE);
  47. lv_obj_add_style(btn2, LV_OBJ_PART_MAIN, &style2);

常用方法

lv_cont_set_layout

  • 设置布局:lv_cont_set_layout(container, LV_LAYOUT_COLUMN_LEFT);
  • LV_LAYOUT_OFF
    • 子对象必须通过lv_obj_set_pos方法设置位置
  • LV_LAYOUT_COLUMN_LEFT
    • 子对象从左边 自上到下排列
  • LV_LAYOUT_PRETTY_TOP

    • 子对象从左到右,从上到下排列,并且每一行均匀分布

      enum {
      LV_LAYOUT_OFF = 0, /**< No layout */
      LV_LAYOUT_CENTER, /**< Center objects */
      
      LV_LAYOUT_COLUMN_LEFT,      /**< Column left align*/
      LV_LAYOUT_COLUMN_MID,       /**< Column middle align*/
      LV_LAYOUT_COLUMN_RIGHT,     /**< Column right align*/
      
      LV_LAYOUT_ROW_TOP,          /**< Row top align*/
      LV_LAYOUT_ROW_MID,          /**< Row middle align*/
      LV_LAYOUT_ROW_BOTTOM,       /**< Row bottom align*/
      
      LV_LAYOUT_PRETTY_TOP,       /**< Row top align*/
      LV_LAYOUT_PRETTY_MID,       /**< Row middle align*/
      LV_LAYOUT_PRETTY_BOTTOM,    /**< Row bottom align*/
      
      LV_LAYOUT_GRID,   /**< Align same-sized object into a grid*/
      
      _LV_LAYOUT_LAST
      };
      typedef uint8_t lv_layout_t;
      

      lv_cont_set_fit

  • 设置大小适应:lv_cont_set_fit(container, LV_FIT_PARENT);

  • LV_FIT_PARENT
    • 填充父容器
  • LV_FIT_TIGHT
    • 包裹子控件大小
      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;