鸿蒙开发系列教程(二十五)--样式处理(一)

1、样式属性
参考网址:https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/ts-universal-attributes-text-style-0000001427902436-V3
在这里插入图片描述

属性方法以 . 链式调用的方式配置系统组件的样式和其他属性

@Entry
@Component
struct Index {
  build() {
    Text('测试')
      .backgroundColor('red')
      .fontSize(50)
      .width('100%')
      .height(100)
  }
}

2、预设枚举值
参考官方网址:https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/ts-appendix-enums-0000001478061741-V3
在这里插入图片描述

@Entry
@Component
struct Index {
  build() {
    Text('测试')
      .fontSize(50)
      .width('100%')
      .height(100)
      .backgroundColor(**Color.Blue**)
      .textAlign(**TextAlign.Center**)
      .fontColor(**Color.White**)
  }
}

3、样式适配

  • vp :鸿蒙默认单位,和屏幕像素有关,最终表现视觉大小在任何设备一致
  • 鸿蒙以伸缩 、网格、栅格进行布局适配,如要等比例缩放可以设置高宽比 aspectRatio

(1)伸缩: layoutWeight(flex: number) 占剩余空间多少份
在这里插入图片描述
加入`layoutWeight参数后,使用layoutWeight属性,让右侧内容去占满剩余宽度:
在这里插入图片描述
(2)内容等比例缩放:可以使用aspectRatio属性设置宽高比
aspectRatio(ratio: number)
在这里插入图片描述
调整aspectRatio值
在这里插入图片描述
参考代码:

@Entry
@Component
struct StyleCase {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Text("我在左面")
      Text("我在右面")
        .textAlign(TextAlign.End)
        .width('80%')
        .height(60)
        .backgroundColor('blue')
        .layoutWeight(1)
      Column()
        .width('50%')
        .height('50%')
        .backgroundColor('red')
        .aspectRatio(1)
    }.width('100%')
    .height('100%')

  }
}
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值