2023-09-25 harmonyos应用开发学习笔记

.ets文件中使用资源时需要注意:
1、百分比的需要新建一个ets文件,export default class XXX { static FULL_WIDTH = 100% }
这样在页面使用时才会生效
2、颜色color的要在color中定义,不能在float等其它地方定义

示例:

@Extend(TextInput) function inputStyle () {
  .height($r('app.float.input_height')) // 这里的height高度是具体的 45vp
  .fontSize($r('app.float.input_font_size')) // 具体的18fp
  /* color的定义要在src/main/resources/base/element/color.json中定义,
   * 或者在element下新建一个xxx.json文件也可以: { "color":[{ "name": "xxxx", "value": "xxx" }]}
 */
  .backgroundColor($r('app.color.input_background_color')) 
  .placeholderColor($r('app.color.placeholder_color'))
  .width(LoginConstant.FULL_WIDTH) // 这个width是100%,需要新建一个文件定义静态值
  .padding({ left: $r('app.float.input_padding_left') })
  .margin({ top: $r('app.float.input_margin_top')})
}

TextInput({ placeholder: $r('app.string.account_placeholder')})
        .maxLength(LoginConstant.ACCOUNT_MAX_LENGTH)
        .inputStyle()

src/main/ets/pages/Login.ets

import LoginConstant from '../common/LoginConstant'

@Extend(TextInput) function inputStyle () {
  .height($r('app.float.input_height'))
  .fontSize($r('app.float.input_font_size'))
  .backgroundColor($r('app.color.input_background_color'))
  .placeholderColor($r('app.color.placeholder_color'))
  .width(LoginConstant.FULL_WIDTH)
  .padding({ left: $r('app.float.input_padding_left') })
  .margin({ top: $r('app.float.input_margin_top')})
}
@Extend(Line) function lineStyle () {
  .width(LoginConstant.FULL_WIDTH)
  .height($r('app.float.line_height'))
  .backgroundColor($r('app.color.line_background_color'))
}
@Extend(Text) function blueText () {
  .fontSize($r('app.float.blue_text_font_size'))
  .fontColor($r('app.color.blue_text_color'))
}

@Entry
@Component

struct Login {
  @Builder loginLogo(img) {
    Image(img)
      .width($r('app.float.logo_width'))
      .height($r('app.float.logo_height'))
      .margin({
        top: $r('app.float.logo_margin_top'),
        bottom: $r('app.float.logo_margin_bottom')
      })
  }
  build() {
    Column() {
      this.loginLogo($r('app.media.login_logo2'))
      Text($r('app.string.login_page'))
        .fontSize($r('app.float.page_title_font_size'))
        .fontWeight(FontWeight.Medium)
        .fontColor($r('app.color.title_text_color'))
      Text($r('app.string.login_page_description'))
        .fontColor($r('app.color.description_text_color'))
        .fontSize($r('app.float.description_font_size'))
        .margin({
          bottom: $r('app.float.description_margin_bottom'),
          top: $r('app.float.description_margin_top')
        })
      TextInput({ placeholder: $r('app.string.account_placeholder')})
        .maxLength(LoginConstant.ACCOUNT_MAX_LENGTH)
        .inputStyle()
      Line().lineStyle()
      TextInput({ placeholder: $r('app.string.password_placeholder')})
        .type(InputType.Password)
        .maxLength(LoginConstant.PASSWORD_MAX_LENGTH)
        .inputStyle()
      Line().lineStyle()
      Row() {
        Text($r('app.string.sms_verification'))
          .blueText()
        Text($r('app.string.forget_password'))
          .blueText()
      }.justifyContent(FlexAlign.SpaceBetween)
      .width(LoginConstant.FULL_WIDTH)
      .margin({ top: $r('app.float.common_margin_top')})
      Button($r('app.string.login_button_text'))
        .width(LoginConstant.LOGIN_BUTTON_WIDTH)
        .margin({ top: $r('app.float.login_button_margin_top') })
      Text($r('app.string.register'))
        .margin({ top: $r('app.float.register_text_margin_top') })
        .fontColor($r('app.color.blue_text_color'))
      Text($r('app.string.other_login_methods'))
        .fontSize($r('app.float.other_method_font_size'))
        .margin({ top: $r('app.float.other_method_margin_top') })
        .fontColor($r('app.color.other_method_text_color'))
      Row() {
        Image($r('app.media.wechat'))
          .width(LoginConstant.OTHER_LOGIN_LOGO_WIDTH)
          .height(LoginConstant.OTHER_LOGIN_LOGO_HEIGHT)
          .margin({ right: $r('app.float.other_method_item_margin_right') })
        Image($r('app.media.qq'))
          .width(LoginConstant.OTHER_LOGIN_LOGO_WIDTH)
          .height(LoginConstant.OTHER_LOGIN_LOGO_HEIGHT)
          .margin({ right: $r('app.float.other_method_item_margin_right') })
        Image($r('app.media.huawei'))
          .width(LoginConstant.OTHER_LOGIN_LOGO_WIDTH)
          .height(LoginConstant.OTHER_LOGIN_LOGO_HEIGHT)
      }
      .margin({ top: $r('app.float.common_margin_top')})
    }
    .backgroundColor($r('app.color.row_background_color'))
    .height(LoginConstant.FULL_HEIGHT)
    .width(LoginConstant.FULL_WIDTH)
    .padding({
      left: $r('app.float.column_padding_left'),
      right: $r('app.float.column_padding_right'),
      bottom: $r('app.float.column_padding_bottom')
    })
  }
}

src/main/ets/common/LoginConstant.ets

export default class LoginConstant {
  // 其它登录方式logo宽度
  static OTHER_LOGIN_LOGO_WIDTH: string = '35vp'

  // 其它登录方式logo高度
  static OTHER_LOGIN_LOGO_HEIGHT: string = '35vp'

  // 全宽度
  static FULL_WIDTH = '100%'

  // 全高度
  static FULL_HEIGHT = '100%'

  // 登录按钮宽度
  static LOGIN_BUTTON_WIDTH = '90%'

  // 帐号最大输入长度
  static ACCOUNT_MAX_LENGTH = 11

  // 密码最大输入长度
  static PASSWORD_MAX_LENGTH = 18
}

src/main/resources/base/element/login_color.json

{
  "color": [
    {
      "name": "input_background_color",
      "value": "#F1F3F5"
    },
    {
      "name": "line_background_color",
      "value": "#33182431"
    },
    {
      "name": "title_text_color",
      "value": "#182431"
    },
    {
      "name": "description_text_color",
      "value": "#99182431"
    },
    {
      "name": "blue_text_color",
      "value": "#007dff"
    },
    {
      "name": "placeholder_color",
      "value": "#99182431"
    },
    {
      "name": "other_method_text_color",
      "value": "#ff8f8f8f"
    },
    {
      "name": "row_background_color",
      "value": "#f1f3f5"
    }
  ]
}

src/main/resources/base/element/login_float.json

{
  "float": [
    {
      "name": "input_height",
      "value": "45vp"
    },
    {
      "name": "input_font_size",
      "value": "18fp"
    },
    {
      "name": "input_padding_left",
      "value": "5vp"
    },
    {
      "name": "input_margin_top",
      "value": "12vp"
    },
    {
      "name": "line_height",
      "value": "1vp"
    },
    {
      "name": "logo_width",
      "value": "78vp"
    },
    {
      "name": "logo_height",
      "value": "78vp"
    },
    {
      "name": "logo_margin_top",
      "value": "100vp"
    },
    {
      "name": "logo_margin_bottom",
      "value": "8vp"
    },
    {
      "name": "page_title_font_size",
      "value": "24fp"
    },
    {
      "name": "description_font_size",
      "value": "16fp"
    },
    {
      "name": "description_margin_top",
      "value": "8vp"
    },
    {
      "name": "description_margin_bottom",
      "value": "30vp"
    },
    {
      "name": "blue_text_font_size",
      "value": "14fp"
    },
    {
      "name": "common_margin_top",
      "value": "12vp"
    },
    {
      "name": "login_button_margin_top",
      "value": "80vp"
    },
    {
      "name": "register_text_margin_top",
      "value": "12vp"
    },
    {
      "name": "other_method_font_size",
      "value": "14fp"
    },
    {
      "name": "other_method_margin_top",
      "value": "130vp"
    },
    {
      "name": "other_method_item_margin_right",
      "value": "42vp"
    },
    {
      "name": "column_padding_left",
      "value": "12vp"
    },
    {
      "name": "column_padding_right",
      "value": "12vp"
    },
    {
      "name": "column_padding_bottom",
      "value": "24vp"
    }
  ]
}

resources的文件 为了区分一下,都新建了一个json文件去管理

image.png

只是简单的记录一下

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容