当前位置:   article > 正文

【“蒙”友会“稿”起来】HarmonyOS 3.1握手Serverless - UI篇_width($r('app.float.iconsize')

width($r('app.float.iconsize')

 前言:

        此帖主要讲解界面开发,有了此UI界面,之后的结合Serverless认证服务、云函数、云数据库、云存储帖子就有面子了^_^ 打算这系列结合Serverless不用端云一体化项目开发,在AGC开通好相关配置,然后下载相关json文件,复制到项目中使用,跟着开发文档一步步配置,从中学习到更多的知识,当然初学者,可以创建端云一体化项目学习、开发,更容易上手,然后再尝试创建一个空项目或带模板项目,手工配置Serverless相关设置,本UI项目是基于Login Ability模板创建的,添加了邮箱注册和手机注册页面。

效果:

讲解

      1. 主界面、包含标题导航组件,登录组件,注册组件,客服与隐私声明组件,其实主界面就是一个组合页面,有各组件拼接而成,关键代码以下:

  1. @Entry
  2. @Component
  3. struct LoginPage {
  4. @Builder NavigationTitle() {
  5. Row() {
  6. Text($r('app.string.title'))
  7. .fontSize($r('app.float.title_text_size'))
  8. .fontColor($r('app.color.title'))
  9. .fontWeight(CommonConstants.TITLE_FONT_WEIGHT)
  10. }
  11. .alignItems(VerticalAlign.Center)
  12. }
  13. build() {
  14. Column() {
  15. // 标题导航组件
  16. Navigation()
  17. .title(this.NavigationTitle())
  18. .hideToolBar(true)
  19. .align(Alignment.Start)
  20. .width(CommonConstants.NAVIGATION_WIDTH_PERCENT)
  21. .height($r('app.float.navigation_height'))
  22. .titleMode(NavigationTitleMode.Mini)
  23. Scroll() {
  24. Column() {
  25. // 登录组件
  26. LoginComponent()
  27. // 占位组件
  28. Blank()
  29. // 邮箱和手机注册入口组件
  30. AuthComponent()
  31. // 隐私声明和客服组件
  32. PrivacyStatementComponent()
  33. }
  34. .height(CommonConstants.SCROLL_HEIGHT_PERCENT)
  35. .constraintSize({ minHeight: $r('app.float.scroll_min_height') })
  36. .alignItems(HorizontalAlign.Start)
  37. }
  38. .layoutWeight(CommonConstants.LOGIN_SCROLL_LAYOUT_WEIGHT)
  39. }
  40. .width(CommonConstants.LOGIN_PAGE_WIDTH_PERCENT)
  41. .backgroundColor($r('app.color.login_page_background'))
  42. }
  43. }
复制

      2. 登录组件, 包含文本框组件、分割线组件、按钮组件、Cloumn容器,关键代码以下:

  1. @Component
  2. export struct LoginComponent {
  3. @State userName: string = '';
  4. @State password: string = '';
  5. build() {
  6. Column() {
  7. // 手机号或邮箱为用户名
  8. TextInput({ placeholder: $r('app.string.username') })
  9. .width(CommonConstants.TEXT_INPUT_WIDTH_PERCENT)
  10. .height($r('app.float.text_input_height'))
  11. .placeholderColor($r('app.color.text_input_place_holder'))
  12. .placeholderFont({ size: $r('app.float.text_input_font_size') })
  13. .backgroundColor($r('app.color.login_input_text_background'))
  14. .fontSize($r('app.float.text_input_font_size'))
  15. .padding({ left: $r('app.float.username_padding_left') })
  16. .onChange((value: string) => {
  17. this.userName = value;
  18. })
  19. // 分割线
  20. Divider()
  21. .width(CommonConstants.DIVIDER_WIDTH_PERCENT)
  22. .height($r('app.float.divider_height'))
  23. .color($r('app.color.divider'))
  24. .alignSelf(ItemAlign.Center)
  25. // 密码框
  26. TextInput({ placeholder: $r('app.string.password') })
  27. .width(CommonConstants.TEXT_INPUT_WIDTH_PERCENT)
  28. .height($r('app.float.text_input_height'))
  29. .placeholderColor($r('app.color.text_input_place_holder'))
  30. .placeholderFont({ size: $r('app.float.text_input_font_size') })
  31. .fontSize($r('app.float.text_input_font_size'))
  32. .backgroundColor($r('app.color.login_input_text_background'))
  33. .type(InputType.Password)
  34. .padding({ left: $r('app.float.password_padding_left') })
  35. .onChange((value: string) => {
  36. this.password = value;
  37. })
  38. // 分割线
  39. Divider()
  40. .width(CommonConstants.DIVIDER_WIDTH_PERCENT)
  41. .height($r('app.float.divider_height'))
  42. .color($r('app.color.divider'))
  43. .alignSelf(ItemAlign.Center)
  44. // 登录按钮
  45. Button($r('app.string.login'))
  46. .width(CommonConstants.BUTTON_WIDTH_PERCENT)
  47. .height($r('app.float.login_btn_height'))
  48. .margin({ top: $r('app.float.login_btn_margin_top') })
  49. .borderRadius($r('app.float.login_btn_border_radius'))
  50. .fontSize($r('app.float.text_input_font_size'))
  51. .fontWeight(CommonConstants.LOGIN_TEXT_FONT_WEIGHT)
  52. .enabled(isLoginButtonClickable(this.userName, this.password))
  53. .fontColor(isLoginButtonClickable(this.userName, this.password)
  54. ? Color.White
  55. : $r('app.color.login_font_normal'))
  56. .backgroundColor(isLoginButtonClickable(this.userName, this.password)
  57. ? $r('app.color.login_btn_enabled')
  58. : $r('app.color.login_btn_normal'))
  59. .onClick(() => {
  60. router.pushUrl({
  61. url: CommonConstants.MINE_PAGE_URL
  62. });
  63. })
  64. }
  65. .padding($r('app.float.login_operation_area_padding'))
  66. }
  67. }
复制

      3. 注册组件, 包含Image组件,Row容器组件,关键代码以下:

  1. @Component
  2. export struct AuthComponent {
  3. build() {
  4. Row() {
  5. // 邮箱注册账号入口图标
  6. Image($r("app.media.ic_mail"))
  7. .width(CommonConstants.ICON_SIZE)
  8. .height(CommonConstants.ICON_SIZE)
  9. .onClick(() => {
  10. router.pushUrl({
  11. url: CommonConstants.EMAIL_PAGE_URL
  12. });
  13. })
  14. // 手机注册账号入口图标
  15. Image($r("app.media.ic_phone"))
  16. .width(CommonConstants.ICON_SIZE)
  17. .height(CommonConstants.ICON_SIZE)
  18. .margin({ left: $r('app.float.icon_space') })
  19. .onClick(() => {
  20. router.pushUrl({
  21. url: CommonConstants.PHONE_PAGE_URL
  22. });
  23. })
  24. }
  25. .width(CommonConstants.ROW_WIDTH_PERCENT)
  26. .justifyContent(FlexAlign.Center)
  27. .margin({bottom: $r('app.float.statement_area_margin_bottom')})
  28. }
  29. }
复制

      4. 客服与隐私声明组件,包含Text文本组件、Flex容器组件,关键代码以下:

  1. @Component
  2. export struct PrivacyStatementComponent {
  3. build() {
  4. Flex({
  5. direction: FlexDirection.Row,
  6. justifyContent: FlexAlign.Center
  7. }) {
  8. // 客服入口
  9. Text($r('app.string.problem_encountered'))
  10. .fontSize($r('app.float.statement_text_size'))
  11. .fontColor($r('app.color.problem_encountered_font'))
  12. .textAlign(TextAlign.Center)
  13. .onClick(() => {
  14. router.pushUrl({
  15. url: CommonConstants.CUSTOMER_SERVICE_PAGE_URL
  16. });
  17. })
  18. // 隐私声明入口
  19. Text($r('app.string.privacy_statement'))
  20. .fontSize($r('app.float.statement_text_size'))
  21. .fontColor($r('app.color.privacy_statement_font'))
  22. .textAlign(TextAlign.Center)
  23. .margin({ left: $r('app.float.statement_space') })
  24. .onClick(() => {
  25. router.pushUrl({
  26. url: CommonConstants.PRIVACY_PAGE_URL
  27. });
  28. })
  29. }
  30. .width(CommonConstants.FLEX_WIDTH_PERCENT)
  31. .margin({
  32. bottom: $r('app.float.statement_area_margin_bottom')
  33. })
  34. }
  35. }
复制

      5. 隐私声明页面,包含导航组件、Image组件、WebView组件,加载本地html文件,关键代码以下:

  1. @Entry
  2. @Component
  3. struct PrivacyPage {
  4. //Webview控制器
  5. webController: WebviewController = new webview.WebviewController();
  6. build() {
  7. Column() {
  8. // 导航组件
  9. Navigation()
  10. .hideToolBar(true)
  11. .height($r('app.float.title_height'))
  12. .width(CommonConstants.NAVIGATION_WIDTH_PERCENT)
  13. .titleMode(NavigationTitleMode.Mini)
  14. // 图片组件
  15. Image($r('app.media.ic_public_privacy'))
  16. .width($r('app.float.icon_length'))
  17. .height($r('app.float.icon_length'))
  18. .objectFit(ImageFit.Contain)
  19. .margin({
  20. top: $r('app.float.icon_margin_top'),
  21. bottom: $r('app.float.icon_margin_bottom')
  22. })
  23. // Webview组件
  24. Web({
  25. src: $rawfile('privacy.html'),
  26. controller: this.webController
  27. })
  28. }
  29. }
  30. }
复制

总结:

       从简单的登录页面布局,学习到了Text组件、TextInput组件、Divider组件、Button组件、Navigation组件、Image组件、Web组件、Flex容器、Row容器、Column容器的使用,只有我们多用组件,它才会为我们的App添加多彩,任何页面都是有组件拼接出来了,当我们学会运用组件,创造组件,页面才更加吸引眼球。有了UI界面,下面集成Serverless的认证服务、云函数、云数据库操作,就更方便了。

进入华为专区,解锁更多内容

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/338808
推荐阅读
相关标签
  

闽ICP备14008679号