布局容器类组件开发指导

    使用场景

    UISwipeView继承UIViewGroup,除提供容器类组件Add、Remove、Insert等方法外还提供按页面滑动功能,滑动结束后当前页面居中对齐显示。该组件分为水平方向和垂直方向,通过Add方法添加的子组件会根据Add的顺序和UISwipeView方向自动水平对齐或则垂直对齐。

    接口说明

    表 1 SwipeView接口说明

    1. 创建一个水平滑动的UISwipeView。

    2. 向UISwipeView中添加子组件。

      1. button1->SetPosition(0, 0, g_ButtonW, g_ButtonH);
      2. button1->SetText("button1");
      3. swipe->Add(button1);
      4. UILabelButton* button2 = new UILabelButton();
      5. button2->SetText("button2");
      6. swipe->Add(button2);
      7. UILabelButton* button3 = new UILabelButton();
      8. button3->SetPosition(0, 0, g_ButtonW, g_ButtonH);
      9. swipe->Add(button3);
    3. 检查实现效果,水平滑动,不可循环。

      图 1 UISwipeView水平滑动效果图

    1. ![](/projects/openharmony-1.0-zh-cn/subsystems/figures/zh-cn_image_0000001053247975.gif)

    开发步骤(水平滑动,可循环)

    1. 创建一个水平滑动的UISwipeView并添加子组件。

    2. 设置UISwipeView循环滑动。

      1. swipe->SetLoopState(true);
    3. 检查实现效果,水平循环滑动。

      图 2 UISwipeView水平滑动循环效果图

    1. ![](/projects/openharmony-1.0-zh-cn/subsystems/figures/zh-cn_image_0000001053207924.gif)

    GridLayout

    接口说明

    表 2 GridLayout接口说明

    方法

    功能

    void SetRows(const uint16_t& rows)

    设置行数

    void SetCols(const uint16_t& cols)

    设置列数

    void LayoutChildren(bool needInvalidate = false)

    布局子组件

    开发步骤

    1. 构造GridLayout并设置位置、大小信息。

    2. 构造子组件button。

      1. UILabelButton* bt1 = new UILabelButton();
      2. bt1->SetText("bt1");
      3. UILabelButton* bt2 = new UILabelButton();
      4. bt2->SetPosition(0, 0, 100, 50);
      5. UILabelButton* bt3 = new UILabelButton();
      6. bt3->SetPosition(0, 0, 100, 50);
      7. bt3->SetText("bt3");
      8. UILabelButton* bt4 = new UILabelButton();
      9. bt4->SetPosition(0, 0, 100, 50);
      10. bt4->SetText("bt4");
    3. 添加子组件并调用LayoutChildren()。

      1. layout_->Add(bt1);
      2. layout_->Add(bt2);
      3. layout_->Add(bt3);
      4. layout_->LayoutChildren();