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

前言:

DevEco Studio版本:4.0.0.600

详细使用介绍

1、Text的一些常用设置

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

TextOverflow属性

效果:

参考文档:OpenHarmony Text显示文本

2、Text文字富文本

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

效果:

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

3、线性边框按钮

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

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

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

效果:

4、Image的一些常用设置

参考链接:OpenHarmony Image图片

圆形图片:

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

图片占位:

Image($r("app.media.startIcon"))
   .width(100)
   .height(100)
   .alt($r("app.media.icon"))

图片加载完成监听:

Image($r("app.media.startIcon"))
   .width(100)
   .height(100)
   .onComplete((event: {
      width: number,
      height: number
   }) => {
      console.info("图片宽度:" + event.width + "图片高度:" + event.height)
   })
参数名类型说明
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

@Component
export struct ImageTest {
   //用于点击“确定”按钮的回调 (API10的写法)
   private onButtonClick: () => void = () => {
   }

   build() {
      Text('确定')
         .fontColor(Color.Black)
         .fontSize('28px')
         .width('146px')
         .height('48px')
         .textAlign(TextAlign.Center)
         .borderColor(Color.Blue)
         .borderWidth('1px')
         .borderRadius('10px')
         .onClick(() => {
            this.onButtonClick()
         })
   }
}

ImageTest的引用

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

6、自定义Dialog弹窗

参考文档:OpenHarmony 自定义弹窗

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

@CustomDialog
export struct MessageDialog {
   build() {
    
   }
}

7、多态样式

参考链接:OpenHarmony 多态样式

stateStyles样式状态:

  • focused:获焦态。

  • normal:正常态。

  • pressed:按压态。

  • disabled:不可用态。

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

@Entry
@Component
struct Index {
   @Styles
   normalStyle() {
      .backgroundColor(Color.White)
   }

   @Styles
   pressedStyle() {
      .backgroundColor(Color.Gray)
   }

   build() {
      Column() {
         Text('确定')
            .fontSize('28px')
            .width('146px')
            .height('48px')
            .textAlign(TextAlign.Center)
            .borderColor(Color.Blue)
            .borderWidth('1px')
            .borderRadius('10px')
            .stateStyles({
               normal: this.normalStyle,
               pressed: this.pressedStyle
            })
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .height('100%')
   }
}

8、日期格式化

@Entry
@Component
struct Index {
   @State message: string = '';

   aboutToAppear() {
      setInterval(() => {
         this.initDate()
      }, 1000)
   }

   initDate() {
      let date = new Date()

      let year = this.format(date.getFullYear()) //获取年份
      let month = this.format(date.getMonth() + 1) //获取月份
      let day = this.format(date.getDate()) //获取天数

      let hours = this.format(date.getHours()) //获取小时
      let minutes = this.format(date.getMinutes()) //获取分钟
      let seconds = this.format(date.getSeconds()) //获取秒数

      this.message = year + '年' + month + '月' + day + '日  ' + hours + ':' + minutes + ':' + seconds
   }

   /**
    * 不足十位前面补零
    */
   format(param: number) {
      let value = '' + param
      if (param < 10) {
         value = '0' + param
      }
      return value
   }

   build() {
      Column() {
         Text(this.message)
            .fontSize(30)//字体大小
            .fontColor(Color.Black)//字体颜色
            .fontWeight(FontWeight.Bold) //字体加粗
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .height('100%')
   }
}

效果:

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

OpenHarmony DateTimeFormat日期格式化

  • 23
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等风起了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值