当前位置: 首页 > news >正文

鸿蒙常用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 {@StylesnormalStyle() {.backgroundColor(Color.White)}@StylespressedStyle() {.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 = '' + paramif (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日期格式化

http://www.lryc.cn/news/287237.html

相关文章:

  • dataGrip连接数据库mysql和intersystems的iris
  • 【51单片机】点亮第一个LED灯
  • ubuntu20.04 格式化 硬盘 扩展硬盘
  • openssl3.2/test/certs - 031 - purpose variants: clientAuth
  • ubuntu下docker卸载和重新安装
  • 搭建k8s集群实战(一)系统设置
  • go-carbon v2.3.6 发布,轻量级、语义化、对开发者友好的 golang 时间处理库
  • 力扣2859-计算k置位下标对应元素的和
  • [计算机提升] 切换(域)用户
  • 蓝桥杯练习题dfs与bfs
  • 软件游戏提示msvcp140.dll丢失的解决方法,全面分析msvcp140.dll文件
  • LandrayOA内存调优 / JAVA内存调优 / Tomcat web.xml 超时时间调优实战
  • 免费SSL数字证书申请,免费数字证书使用教程
  • 深入理解Flutter中的GlobalKey与LocalKey(ValueKey、ObjectKey、UniqueKey)及其使用方法
  • linux命令学习
  • 核桃的数量---蓝桥杯
  • 进程通信与socket编程实践之猜数字小游戏
  • AcWing 1241. 外卖店优先级(复杂模拟思路 + 代码详解)
  • 查询文件hash值
  • [docker] Docker资源管理
  • 不就业,纯兴趣,应该自学C#还是JAVA?
  • 【Go面试向】defer与time.sleep初探
  • fpga外置flash程序烧录流程
  • 什么是通配监听端口? 什么是通配监听IP?
  • CentOS 安装 Ruby
  • Laya3.0 相机使用
  • 前端语音识别(webkitSpeechRecognition)
  • Flutter中状态管理选项的比较:利弊探索
  • # [NOI2019] 斗主地 洛谷黑题题解
  • 踩坑(6)Redisson调用unlockAsync方法释放锁失败