当前位置:   article > 正文

HarmonyOS 开发

HarmonyOS 开发
环境 

下载IDE

代码 
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import testNapi from 'libentry.so';
  3. import { router } from '@kit.ArkUI';
  4. import { common, Want } from '@kit.AbilityKit';
  5. @Entry
  6. @Component
  7. struct Index {
  8. @State message: string = 'Hello HarmonyOS!';
  9. private context = getContext(this) as common.UIAbilityContext;
  10. // Ability间跳转
  11. async explicitStartAbility() {
  12. const want : Want = {
  13. deviceId: "",
  14. bundleName: "com.example.board",
  15. moduleName: "entry",
  16. abilityName: "secondAbitily"
  17. };
  18. let context = getContext(this) as common.UIAbilityContext;
  19. await context.startAbility(want);
  20. }
  21. build() {
  22. Row() {
  23. Column() {
  24. Text(this.message)
  25. .fontSize(50)
  26. .fontWeight(FontWeight.Bold)
  27. .fontColor($r('app.color.text_color'))//访问字体颜色
  28. .backgroundImage($r('app.media.background'), ImageRepeat.XY) //设置背景图片,ImageRepeat.XY则是图片太小时候,选择某个坐标位置的颜色填充
  29. .onClick(() => {
  30. hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
  31. })
  32. .margin({ top: 20 })
  33. Button('Click me')
  34. .onClick(() => {
  35. this.message = 'Hello, World!';
  36. })
  37. .margin({ top: 10 })
  38. Button('Next Page')
  39. .onClick(() => {
  40. router.pushUrl({url: "pages/flex",
  41. params: {name: "args"}},
  42. router.RouterMode.Single) // 同ability
  43. // this.explicitStartAbility(); // 不同ability
  44. })
  45. // 使用系统资源
  46. Image($r('sys.media.ohos_app_icon'))
  47. .border({
  48. color: $r('sys.color.ohos_id_color_palette_aux11'), // 设置边框颜色为辅助色11
  49. radius: $r('sys.float.ohos_id_corner_radius_button'), // 设置边框圆角半径为按钮圆角半径
  50. width: 2 // 设置边框宽度为2
  51. })
  52. .margin({
  53. top: $r('sys.float.ohos_id_elements_margin_horizontal_m'), // 设置上边距为水平中等间距
  54. bottom: $r('sys.float.ohos_id_elements_margin_horizontal_l') // 设置下边距为水平大间距
  55. })
  56. .height(200) // 设置高度为200
  57. .width(300)
  58. }
  59. .width('100%')
  60. }
  61. .height('100%')
  62. .justifyContent(FlexAlign.Center)
  63. .alignItems(VerticalAlign.Center)
  64. .padding(10)
  65. }
  66. }

 

  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import testNapi from 'libentry.so';
  3. import { router } from '@kit.ArkUI';
  4. @Entry
  5. @Component
  6. struct Example {
  7. @State name : string = (router.getParams() as Record<string, string>)['name'];
  8. build() {
  9. Column() {
  10. Button('Prev Page')
  11. .onClick(() => {
  12. router.back() // 同ability跳转
  13. // router.pushUrl({url: "pages/Index"})
  14. })
  15. /**Flex({ wrap: FlexWrap.Wrap })是一个Flex布局的设置,
  16. *其中wrap: FlexWrap.Wrap表示设置Flex容器的子元素在主轴方向上超出容器时是否换行。
  17. *在这里,FlexWrap.Wrap表示子元素会自动换行,以适应容器的尺寸。
  18. * 这样设置可以确保在容器尺寸不足以容纳所有子元素时,子元素会自动换行,而不会超出容器范围。
  19. */
  20. Flex({ wrap: FlexWrap.Wrap }) {
  21. // 默认不写单位就是是vp
  22. Column() {
  23. Text("width(220)")
  24. .width(220) // 设置宽度为220vp
  25. .height(40) // 设置高度为40vp
  26. .backgroundColor("#00BFC9") // 设置背景颜色为#00BFC9
  27. .fontSize("12vp") // 设置字体大小为12vp
  28. }.margin(5) // 设置外边距为5vp
  29. // 宽度指定成px
  30. Column() {
  31. Text("width('220px')")
  32. .width('220px') // 设置宽度为220px
  33. .height(40) // 设置高度为40vp
  34. .backgroundColor("#007900") // 设置背景颜色为#007900
  35. .textAlign(TextAlign.Center) // 设置文本对齐方式为居中
  36. .fontColor(Color.White) // 设置字体颜色为白色
  37. }.margin(5) // 设置外边距为5vp
  38. // 宽度指定成vp
  39. Column() {
  40. Text("width('220vp')")
  41. .width('220vp') // 设置宽度为220vp
  42. .height(40) // 设置高度为40vp
  43. .backgroundColor("#FF9800") // 设置背景颜色为#FF9800
  44. .textAlign(TextAlign.Center) // 设置文本对齐方式为居中
  45. .fontColor(Color.White) // 设置字体颜色为白色
  46. .fontSize('12vp') // 设置字体大小为12vp
  47. }.margin(5) // 设置外边距为5vp
  48. // 宽度指定成vplpx
  49. Column() {
  50. Text("width('220lpx') designWidth:720")
  51. .width("220lpx") // 设置宽度为220lpx
  52. .height(40) // 设置高度为40vp
  53. .backgroundColor("#634794") // 设置背景颜色为#634794
  54. .textAlign(TextAlign.Center) // 设置文本对齐方式为居中
  55. .fontColor(Color.White) // 设置字体颜色为白色
  56. .fontSize('12vp') // 设置字体大小为12vp
  57. }.margin(5) // 设置外边距为5vp
  58. // 将vp单位的数值转换为以px为单位的数值
  59. Column() {
  60. Text("width(vp2px(220) + 'px')")
  61. .width(vp2px(220) + 'px') // 将220vp转换为px单位的数值,然后设置宽度
  62. .height(40) // 设置高度为40vp
  63. .backgroundColor('#3F56EA') // 设置背景颜色为#3F56EA
  64. .textAlign(TextAlign.Center) // 设置文本对齐方式为居中
  65. .fontColor(Color.White) // 设置字体颜色为白色
  66. .fontSize('12vp') // 设置字体大小为12vp
  67. }.margin(5) // 设置外边距为5vp
  68. // fontSize('12fp')设置成fp
  69. Column() {
  70. Text("fontSize('12fp')")
  71. .width(220) // 设置宽度为220vp
  72. .height(40) // 设置高度为40vp
  73. .backgroundColor('#A14A5C') // 设置背景颜色为#A14A5C
  74. .textAlign(TextAlign.Center) // 设置文本对齐方式为居中
  75. .fontColor(Color.White) // 设置字体颜色为白色
  76. .fontSize('12fp') // 设置字体大小为12fp
  77. }.margin(5) // 设置外边距为5vp
  78. // 将px单位的数值转换为以vp为单位的数值。
  79. Column() {
  80. Text("width(px2vp(220))")
  81. .width(px2vp(220)) // 将220px转换为vp单位的数值,然后设置宽度
  82. .height(40) // 设置高度为40vp
  83. .backgroundColor('#E40078') // 设置背景颜色为#E40078
  84. .textAlign(TextAlign.Center) // 设置文本对齐方式为居中
  85. .fontColor(Color.White) // 设置字体颜色为白色
  86. .fontSize('12fp') // 设置字体大小为12fp
  87. }.margin(5) // 设置外边距为5vp
  88. }.width('100%') // 设置宽度为100%
  89. }
  90. }
  91. }
效果

参考

快速入门 - 华为开发者联盟


创作不易,小小的支持一下吧!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号