当前位置:   article > 正文

鸿蒙常用UI效果及一些处理方式总结_openharmony ui 美化

openharmony ui 美化

前言:

DevEco Studio版本:4.0.0.600

详细使用介绍

1、Text的一些常用设置

  1. Text(this.message)
  2. .fontSize(50)//字体大小
  3. .fontColor(Color.White)//字体颜色
  4. .fontWeight(FontWeight.Bold)//字体加粗
  5. .backgroundColor(Color.Black)//背景颜色
  6. .fontStyle(FontStyle.Italic) //字体倾斜
  7. .textOverflow({overflow: TextOverflow.Ellipsis})//文本超长显示方式,TextOverflow.Ellipsis:以省略号结尾
  8. .maxLines(1)//设置文本的最大行数

TextOverflow属性

效果:

参考文档:OpenHarmony Text显示文本

2、Text文字富文本

  1. Text() {
  2. Span("《用户协议》").fontColor(Color.Blue)
  3. .decoration({ type: TextDecorationType.Underline, color: Color.Blue })//设置文本装饰线样式及其颜色。
  4. .onClick(() => {
  5. //实现点击,跳转到用户协议界面
  6. })
  7. }

效果:

参考文档:OpenHarmony span富文本设置

3、线性边框按钮

  1. Text('确定')
  2. .fontColor(Color.Black)
  3. .fontSize('28px')
  4. .width('146px')
  5. .height('48px')
  6. .textAlign(TextAlign.Center)
  7. .borderColor(Color.Blue)//边框颜色
  8. .borderWidth('1px')//边框宽度
  9. .borderRadius('10px')//边框圆角
  10. .onClick(() => {
  11. //实现点击逻辑
  12. })

// 如果单独设置某一个圆角可以通过下面方式设置,topLeft:左上角,topRight:右上角,bottomLeft:左下角,bottomRight:右下角

.borderRadius({ topLeft: '10px', topRight: '10px', bottomLeft: '10px', bottomRight: '10px' })

效果:

4、Image的一些常用设置

参考链接:OpenHarmony Image图片

圆形图片:

  1. Image($r("app.media.startIcon"))
  2. .width(100)
  3. .height(100)
  4. .clip(new Circle({ width: 100, height: 100 }))

图片占位:

  1. Image($r("app.media.startIcon"))
  2. .width(100)
  3. .height(100)
  4. .alt($r("app.media.icon"))

图片加载完成监听:

  1. Image($r("app.media.startIcon"))
  2. .width(100)
  3. .height(100)
  4. .onComplete((event: {
  5. width: number,
  6. height: number
  7. }) => {
  8. console.info("图片宽度:" + event.width + "图片高度:" + event.height)
  9. })
参数名类型说明
widthnumber图片的宽。
单位:像素
heightnumber图片的高。
单位:像素
componentWidthnumber组件的宽。
单位:像素
componentHeightnumber组件的高。
单位:像素
loadingStatusnumber图片加载成功的状态值。
说明:
返回的状态值为0时,表示图片数据加载成功。返回的状态值为1时,表示图片解码成功。
contentWidth(10+)number图片实际绘制的宽度。
单位:像素
说明:
仅在loadingStatus返回1时有效。
contentHeight(10+)number图片实际绘制的高度。
单位:像素
说明:
仅在loadingStatus返回1时有效。
contentOffsetX(10+)number实际绘制内容相对于组件自身的x轴偏移。
单位:像素
说明:
仅在loadingStatus返回1时有效。
contentOffsetY(10+)number实际绘制内容相对于组件自身的y轴偏移。
单位:像素
说明:
仅在loadingStatus返回1时有效。

5、接口回调

自定义一个View:ImageTest

  1. @Component
  2. export struct ImageTest {
  3. //用于点击“确定”按钮的回调 (API10的写法)
  4. private onButtonClick: () => void = () => {
  5. }
  6. build() {
  7. Text('确定')
  8. .fontColor(Color.Black)
  9. .fontSize('28px')
  10. .width('146px')
  11. .height('48px')
  12. .textAlign(TextAlign.Center)
  13. .borderColor(Color.Blue)
  14. .borderWidth('1px')
  15. .borderRadius('10px')
  16. .onClick(() => {
  17. this.onButtonClick()
  18. })
  19. }
  20. }

ImageTest的引用

  1. ImageTest({ onButtonClick: () => {
  2. promptAction.showToast({
  3. message: '我是回调的数据',
  4. duration: 2000,
  5. })
  6. } })

6、自定义Dialog弹窗

参考文档:OpenHarmony 自定义弹窗

使用@CustomDialog装饰器装饰自定义弹窗

  1. @CustomDialog
  2. export struct MessageDialog {
  3. build() {
  4. }
  5. }

7、多态样式

参考链接:OpenHarmony 多态样式

stateStyles样式状态:

  • focused:获焦态。

  • normal:正常态。

  • pressed:按压态。

  • disabled:不可用态。

  • selected:选中态。(API 10中新增)

  1. @Entry
  2. @Component
  3. struct Index {
  4. @Styles
  5. normalStyle() {
  6. .backgroundColor(Color.White)
  7. }
  8. @Styles
  9. pressedStyle() {
  10. .backgroundColor(Color.Gray)
  11. }
  12. build() {
  13. Column() {
  14. Text('确定')
  15. .fontSize('28px')
  16. .width('146px')
  17. .height('48px')
  18. .textAlign(TextAlign.Center)
  19. .borderColor(Color.Blue)
  20. .borderWidth('1px')
  21. .borderRadius('10px')
  22. .stateStyles({
  23. normal: this.normalStyle,
  24. pressed: this.pressedStyle
  25. })
  26. }
  27. .justifyContent(FlexAlign.Center)
  28. .width('100%')
  29. .height('100%')
  30. }
  31. }

8、日期格式化

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State message: string = '';
  5. aboutToAppear() {
  6. setInterval(() => {
  7. this.initDate()
  8. }, 1000)
  9. }
  10. initDate() {
  11. let date = new Date()
  12. let year = this.format(date.getFullYear()) //获取年份
  13. let month = this.format(date.getMonth() + 1) //获取月份
  14. let day = this.format(date.getDate()) //获取天数
  15. let hours = this.format(date.getHours()) //获取小时
  16. let minutes = this.format(date.getMinutes()) //获取分钟
  17. let seconds = this.format(date.getSeconds()) //获取秒数
  18. this.message = year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes + ':' + seconds
  19. }
  20. /**
  21. * 不足十位前面补零
  22. */
  23. format(param: number) {
  24. let value = '' + param
  25. if (param < 10) {
  26. value = '0' + param
  27. }
  28. return value
  29. }
  30. build() {
  31. Column() {
  32. Text(this.message)
  33. .fontSize(30)//字体大小
  34. .fontColor(Color.Black)//字体颜色
  35. .fontWeight(FontWeight.Bold) //字体加粗
  36. }
  37. .justifyContent(FlexAlign.Center)
  38. .width('100%')
  39. .height('100%')
  40. }
  41. }

效果:

或者通过@ohos.intl (国际化-Intl)来实现,参考文档:

OpenHarmony DateTimeFormat日期格式化

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

闽ICP备14008679号