鸿蒙(HarmonyOS)项目方舟框架(ArkUI)控件的部分公共属性和事件

news/2024/7/21 9:57:55 标签: harmonyos

鸿蒙(HarmonyOS)项目方舟框架(ArkUI)控件的部分公共属性和事件

一、操作环境

操作系统:  Windows 10 专业版

IDE:DevEco Studio 3.1

SDK:HarmonyOS 3.1

二、公共属性

常用的公共属性有:

宽(with)、高(height)、尺寸(size)、背景色(backgroudColor)、

Text()
  .size({width: 220, height: 125}) // 设置宽高
  .width(120)                      // 设置宽度,覆盖前边的值
  .height(25)                      // 设置高度,覆盖前边的值
  .backgroundColor("#ccbbaa")      // 设置背景色

设置组件的宽高,缺省时使用组件自身内容的宽高,比如充满父布局可以使用 string 值:"100%",当组件同时设置 size 和 width / height 时,以最后设置的值为准。

外边距(padding)、内边距(margin)、

Stack() {
  Text()
    .width('100%')               // 设置宽度充满父布局
    .height('100%')              // 设置高度充满父布局
    .backgroundColor(Color.Pink) // 设置背景色
}
.padding(10)                     // 设置四个边距值
.backgroundColor("#aabbcc")      // 设置背景色
.size({width: 80, height: 80})   // 设置宽高尺寸

Stack() {
  Text()
    .width('100%')               // 宽度充满父布局
    .height('100%')              // 高度充满父布局
    .backgroundColor(Color.Pink) // 设置背景色
}
.padding({left: 5, top: 20, right: 5, bottom: 20})// 设置不同的边距值
.backgroundColor("#aabbcc")                       // 设置背景色
.size({width: 80, height: 80})                    // 设置宽高尺寸

权重(layoutWeight)、对齐方式(align)、布局方向(direction对应的枚举
Ltr,Rtl,Auto)、相对定位(offset)、绝对定位(position)、

显示隐藏(visibility对应的枚举Visible,Hidden,None)

Row() {
  Text()
    .height(30)
    .width(120)
    .backgroundColor("#aabbcc")
    .layoutWeight(1)

  Text()
    .height(30)
    .backgroundColor("#aaccbb")
    .visibility(Visibility.Visible) // 设置默认值Visible
    .layoutWeight(1)

  Text()
    .height(30)
    .backgroundColor(Color.Pink)
    .layoutWeight(1)
}

Row() {
  Text()
    .height(30)
    .width(120)
    .backgroundColor("#aabbcc")
    .layoutWeight(1)

  Text()
    .height(30)
    .backgroundColor("#aaccbb")
    .visibility(Visibility.Hidden) // 设置Hidden,不在界面显示但是还占着位置
    .layoutWeight(1)

  Text()
    .height(30)
    .backgroundColor(Color.Pink)
    .layoutWeight(1)
}

Row() {
  Text()
    .height(30)
    .backgroundColor("#aabbcc")
    .layoutWeight(1)

  Text()
    .height(30)
    .visibility(Visibility.None) // 设置None,不在界面上显示
    .backgroundColor("#aaccbb")
    .layoutWeight(1)

  Text()
    .height(30)
    .backgroundColor(Color.Pink)
    .layoutWeight(1)
}

三、公共事件

常用的公共事件:

点击事件(onClick)

Text('Click 亚丁号')
  .width(120)
  .height(40)
  .backgroundColor(Color.Pink)      // 设置背景颜色
  .onClick(() => {                  // 设置点击事件回调
    console.log("text clicked 亚丁号") // 日志输出
  })

获得焦点事件、失去焦点事件

@Entry @Component struct ComponentTest {

  @State textOne: string = ''
  @State textTwo: string = ''
  @State textThree: string = ''
  @State oneButtonColor: string = '#FF0000'
  @State twoButtonColor: string = '#87CEFA'
  @State threeButtonColor: string = '#90EE90'

  build() {
    Column({ space: 10 }) {
      Button(this.textOne)
        .backgroundColor(this.oneButtonColor)
        .width(260)
        .height(70)
        .fontColor(Color.Black)
        .focusable(true)
        .onFocus(() => {
          this.textOne = 'First Button onFocus'
          this.oneButtonColor = '#AFEEEE'
        })
        .onBlur(() => {
          this.textOne = 'First Button onBlur'
          this.oneButtonColor = '#FFC0CB'
        })
      Button(this.textTwo)
        .backgroundColor(this.twoButtonColor)
        .width(260)
        .height(70)
        .fontColor(Color.Black)
        .focusable(true)
      Button(this.textThree)
        .backgroundColor(this.threeButtonColor)
        .width(260)
        .height(70)
        .fontColor(Color.Black)
        .focusable(true)
        .onFocus(() => {
          this.textThree = 'Third Button onFocus'
          this.threeButtonColor = '#AFEEEE'
        })
        .onBlur(() => {
          this.textThree = 'Third Button onBlur'
          this.threeButtonColor = '#FFC0CB'
        })
    }
    .width('100%')
    .height('100%')
    .padding(10)
  }
}

目前支持焦点事件的组件:Button、 TextImage、 List、 Grid

好了就写到这吧!

你有时间常去我家看看我在这里谢谢你啦...

我家地址:亚丁号

最后送大家一首诗:

山高路远坑深,
大军纵横驰奔,

谁敢横刀立马?
惟有点赞加关注大军。


http://www.niftyadmin.cn/n/5273908.html

相关文章

高空作业MR混合现实情景实训教学应用

MR混合现实情景实训教学系统结合了虚拟现实和增强现实的优点,能够提供一种真实而丰富的环境,使学习者可以在其中进行模拟的高空作业。通过精确的传感器和高级算法,捕捉到学生的动作和环境变化,从而实时调整虚拟环境的反馈&#xf…

scala学习四:方法与函数

一、 方法与函数 Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量。换句话来说在类中定义的函数即是方法。 Scala中val语句可以定义函数,def语句定义方法 class Test{def m(x: Int) x3val f (x:Int) > x3 }1.1 方法声明 def functio…

centos nginx 安装 stream 模块

查看nginx编译配置 ./nginx -V输出如下 configure arguments: --usernginx --groupnginx --prefix/opt/nginx --with-http_stub_status_module --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-openssl/home/migu/openssl-1.0.2l 发现没有stream模块 …

Python基础练习题附带解题思路与代码

题目1 编写一个Python程序,要求实现一个猜数字游戏,玩家有10次机会猜测一个1到100之间的随机数,如果猜对了,程序会输出“恭喜你,猜对了!”;如果猜错了,程序会提示玩家还剩下多少次机…

react点击事件

使用事件对象传参 //按钮 <button onClick{thisClick}>点击事件</button> //方法 const thisClick (e) >{console.log(这里是点击结果,e)} 输出结果 传递自定义参数 //按钮 <button onClick{()>thisClick(参数)}>点击事件</button> //方法 c…

长短期记忆(LSTM)神经网络-多输入分类

目录 一、程序及算法内容介绍&#xff1a; 基本内容&#xff1a; 亮点与优势&#xff1a; 二、实际运行效果&#xff1a; 三、部分程序&#xff1a; 四、完整程序下载&#xff1a; 一、程序及算法内容介绍&#xff1a; 基本内容&#xff1a; 本代码基于Matlab平台编译&am…

换热站数字孪生 | 图扑智慧供热 3D 可视化

换热站作为供热系统不可或缺的一部分&#xff0c;其能源消耗对城市环保至关重要。在双碳目标下&#xff0c;供热企业可通过搭建智慧供热系统&#xff0c;实现供热方式的低碳、高效、智能化&#xff0c;从而减少碳排放和能源浪费。通过应用物联网、大数据等高新技术&#xff0c;…

Docker使用6-Multi container apps

写在前面 主题是Multi container apps&#xff0c;这里是链接。本文主要学习使用容器与其他容器的关联。 Container networking 当你的程序需要用到另一个程序怎么办&#xff0c;例如app需要用到mysql。容器内的一切都是单独的&#xff0c;与其他容器不关联&#xff0c;那要…