鸿蒙OS 组件与布局代码创建布局

2020-09-18 11:51 更新

开发如下图所示界面,需要添加一个 Text 组件和 Button 组件。由于两个组件从上到下依次居中排列,可以选择使用竖向的 DirectionalLayout 布局来放置组件。

图1 开发样例图 img

代码创建布局需要分别创建组件和布局,并将它们进行组织关联。

创建组件

  1. 声明组件

  1. Button button = new Button(context); // 参数context表示AbilitySlice的Context对象

  1. 设置组件大小

  1. button.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
  2. button.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);

  1. 设置组件属性及ID

  1. button.setText("My name is Button.");
  2. button.setTextSize(25);
  3. button.setId(ID_BUTTON);

创建布局并使用

  1. 声明布局

  1. DirectionalLayout directionalLayout = new DirectionalLayout(context);

  1. 设置布局大小

  1. directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
  2. directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);

  1. 设置布局属性及 ID

  1. directionalLayout.setOrientation(Component.VERTICAL);

  1. 将组件添加到布局中(视布局需要对组件设置布局属性进行约束)

  1. directionalLayout.addComponent(button);

  1. 将布局添加到组件树中

  1. setUIContent(directionalLayout);

完整代码示例:

  1. @Override
  2. public void onStart(Intent intent) {
  3. super.onStart(intent);
  4. // 步骤1 声明布局
  5. DirectionalLayout directionalLayout = new DirectionalLayout(context);
  6. // 步骤2 设置布局大小
  7. directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
  8. directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
  9. // 步骤3 设置布局属性及ID(ID视需要设置即可)
  10. directionalLayout.setOrientation(Component.VERTICAL);
  11. directionalLayout.setPadding(32, 32, 32, 32);
  12. Text text = new Text(context);
  13. text.setText("My name is Text.");
  14. text.setTextSize(50);
  15. text.setId(100);
  16. // 步骤4.1 为组件添加对应布局的布局属性
  17. DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT,
  18. LayoutConfig.MATCH_CONTENT);
  19. layoutConfig.alignment = LayoutAlignment.HORIZONTAL_CENTER;
  20. text.setLayoutConfig(layoutConfig);
  21. // 步骤4.2 将Text添加到布局中
  22. directionalLayout.addComponent(text);
  23. // 类似的添加一个Button
  24. Button button = new Button(context);
  25. layoutConfig.setMargins(0, 50, 0, 0);
  26. button.setLayoutConfig(layoutConfig);
  27. button.setText("My name is Button.");
  28. button.setTextSize(50);
  29. button.setId(100);
  30. ShapeElement background = new ShapeElement();
  31. background.setRgbColor(new RgbColor(0, 125, 255));
  32. background.setCornerRadius(25);
  33. button.setBackground(background);
  34. button.setPadding(10, 10, 10, 10);
  35. button.setClickedListener(new Component.ClickedListener() {
  36. @Override
  37. // 在组件中增加对点击事件的检测
  38. public void onClick(Component Component) {
  39. // 此处添加按钮被点击需要执行的操作
  40. }
  41. });
  42. directionalLayout.addComponent(button);
  43. // 步骤5 将布局作为根布局添加到视图树中
  44. super.setUIContent(directionalLayout);
  45. }

根据以上步骤创建组件和布局后的界面显示效果如[图1]所示。其中,代码示例中为组件设置了一个按键回调,在按键被按下后,应用会执行自定义的操作。

在代码示例中,可以看到设置组件大小的方法有两种:

  • 通过 setWidth/setHeight 直接设置宽高。
  • 通过 setLayoutConfig 方法设置布局属性来设定宽高。

这两种方法的区别是后者还可以增加更多的布局属性设置,例如:使用“alignment”设置水平居中的约束。另外,这两种方法设置的宽高以最后设置的作为最终结果。它们的取值一致,可以是以下取值:

  • 具体以像素为单位的数值。
  • MATCH_PARENT:表示组件大小将扩展为父组件允许的最大值,它将占据父组件方向上的剩余大小。
  • MATCH_CONTENT:表示组件大小与它内容占据的大小范围相适应。
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号