鸿蒙系列--组件介绍之基础组件
创始人
2024-11-10 03:37:42
0

一、通用属性和文本样式 

        针对包含文本元素的组件(比如:Text、Span、Button、TextInput等),可以设置一些通用的文本样式,比如颜色:fontColor、大小:fontSize、样式:fontStyle、 粗细:fontWeight、字体:fontFamily

二、Text

文本组件,可以包含子组件Span

1.基础用法

@Entry @Component struct TextPage {   build() {     Row() {       Column() {         Text("默认样式Text组件")         Text("可设置基础文本样式")           .fontColor(Color.Red)           .fontStyle(FontStyle.Italic)           .fontSize(30)           .fontWeight(FontWeight.Bold)           .fontFamily("Arial,sans-serif")       }       .width('100%')     }     .height('100%')   } }

2.常用属性

textAlign:
@Entry @Component struct TextPage {    build() {     Row() {       Column() {         Text("水平对齐尾部")           .width('100%')           .backgroundColor(Color.Yellow)           .textAlign(TextAlign.End)       }       .width('100%')     }     .height('100%')   } }

        设置文本的对齐方式

参数类型为TextAlign,可选类型:

  • Start:水平对齐首部(默认)
  • Center:水平居中对齐
  • End:水平对齐尾部

maxLines:

        用于设置文本显示最大行数。

textOverflow:

        设置文本截取方式

    .textOverflow({overflow:TextOverflow.Ellipsis})     .maxLines(1)

        当文本内容较多超出了组件范围时,用来设置文本截取方式,需配合maxLines使用,单独设置不生效

参数类型为TextOverflow,可设置类型:

  • None:无
  • Clip:直接剪裁
  • Ellipsis:将显示不下的文本用 “...” 表示

decoration:

        设置文本装饰线样式及其颜色

 .decoration({ type: TextDecorationType.Underline, color: Color.Red })

设置文本装饰线样式,使用TextDecorationType设置,可设置的类型:

  • None:不使用文本装饰线
  • Overline:文字上划线修饰
  • LineThrough:穿过文本的修饰线,中划线
  • Underline:文字下划线修饰。

使用Color设置本文装饰线颜色

三、Image

图片组件,可以给Image设置图片地址、宽和高

1.基础用法

@Entry @Component struct ImagePage {    build() {     Row() {       Column() {         Image($r("app.media.icon"))           .width(100)           .height(100)       }       .width('100%')     }     .height('100%')   } }

2.常用属性

objectFit

        设置图片的缩放类型

    .objectFit(ImageFit.Contain)

使用ImageFit设置,可设置类型:

  • Cover:保持宽高比进行缩小或者放大,使得图片两边都大于或等于显示边界。默认
  • Contain:保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内
  • Auto:自适应显示
  • Fill:不保持宽高比进行放大缩小,使得图片充满显示边界
  • ScaleDown:保持宽高比显示,图片缩小或者保持不变
  • None:保持原有尺寸显示

3.加载网络图片

 Image('https://网络图片.jpg')

        需要在module.json5文件中申明网络访问权限

{     "module" : {         "requestPermissions":[            {              "name": "ohos.permission.INTERNET"            }         ]     } }

四、TextInput

输入单行文本

1.基础用法

@Entry @Component struct TextInputPage {    build() {     Row() {       Column() {         TextInput()           .fontColor(Color.Blue)           .fontSize(20)           .fontStyle(FontStyle.Italic)           .fontWeight(FontWeight.Bold)           .fontFamily('Arial')       }       .width('100%')     }     .height('100%')   } }

2.设置提示文本:placeholder      

@Entry @Component struct TextInputPage {   build() {     Row() {       Column() {         TextInput({ placeholder: '请输入账号' })           .placeholderColor(0x999999)           .placeholderFont({ size: 20, weight: FontWeight.Medium, family: 'cursive', style: FontStyle.Italic })       }       .width('100%')     }     .height('100%')   } }
  • placeholderColor:设置提示文本的颜色
  • placeholderFont:设置提示文本的样式,可以设置文本大小size,粗细weight,样式style,字体family

3.设置输入类型:type

        type的参数类型为InputType,包含的输入类型:

  • Normal:基本输入模式。支持输入数字、字母、下划线、空格、特殊字符
  • Password:密码输入模式
  • Email:e-mail地址输入模式
  • Number:纯数字输入模式

4.设置光标位置

        使用TextInputController的caretPosition方法

创建一个TextInputController对象,将这个对象设置到TextInput组件中,当点击按钮时,如果输入的字符数满足条件,光标会移动到caretPosition设置的position中

@Entry @Component struct TextInputPage {   controller: TextInputController = new TextInputController()    build() {     Row() {       Column() {         TextInput({ controller: this.controller })           .type(InputType.Email)          Button('设置光标位置')           .onClick(() => {             this.controller.caretPosition(2)           })       }       .width('100%')     }     .height('100%')   } }

5.获取输入文本

可以给TextInput设置onChange事件,输入文本发生变化时触发回调

@Entry @Component struct TextInputPage {   @State text: string = ''    build() {     Row() {       Column() {         TextInput({ placeholder: '请输入账号' })           .onChange((value:string) => {             this.text = value           })          Text(this.text)       }       .width('100%')     }     .height('100%')   } }

6.其他属性

caretColor:

修改光标颜色

    .caretColor(Color.Red) 

五、Button

用来响应点击事件操作,可以包含子组件

1.基础用法

实现一个登录按钮

@Entry @Component struct ButtonPage {    build() {     Row() {       Column() {         Button('登录', { type: ButtonType.Capsule, stateEffect: true })           .width('90%')           .height(40)           .fontSize(16)           .fontWeight(FontWeight.Medium)           .backgroundColor('#007DFF')       }       .width('100%')     }     .height('100%')   } }

2.设置按钮样式:type

用ButtonType来设置按钮样式,可设置的类型有:

  • Capsule:胶囊型按钮(圆角默认为高度的一半)
  • Circle:圆形按钮
  • Normal:普通按钮(默认不带圆角)

用stateEffect来设置按钮是否开启切换效果(闪动),false时,点击效果关闭,默认值为true。

    Button('登录', { type: ButtonType.Capsule, stateEffect: true }) 

3.设置按钮点击事件

用onClick来给按钮绑定点击事件

      .onClick(()=>{             // 处理点击事件逻辑        })

4.设置子组件

@Entry @Component struct ButtonPage {   build() {     Row() {       Column() {         Button({ type: ButtonType.Capsule, stateEffect: true }) {           Row() {             Image($r('app.media.icon'))               .width(30)               .height(30)              Text('登录')               .fontSize(20)               .fontColor(Color.White)           }         }         .width('90%')         .height(40)         .backgroundColor(Color.Red)       }       .width('100%')     }     .height('100%')   } }

六、LoadingProgress

进度条组件

可以设置颜色color,和宽width、高height        

@Entry @Component struct LoadingProgressPage {    build() {     Row() {       Column() {         LoadingProgress()           .color(Color.Blue)           .height(60)           .width(60)       }       .width('100%')     }     .height('100%')   } }

相关内容

热门资讯

秒懂普及”珊瑚互娱房卡领取码“... 秒懂普及”珊瑚互娱房卡领取码“王者大厅房间卡怎么购买游戏中心打开微信,添加客服【113857776】...
秒懂教程!我买微信牛牛房卡链接... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:71319951许多玩家在游戏中会购买房卡来享受...
正规平台有哪些,怎么买斗牛房卡... 微信游戏中心:火神大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
给大家讲解“购买斗牛房卡联系方... 起点大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
ia攻略/游戏推荐斗牛房卡出售... ia攻略/游戏推荐斗牛房卡出售长虹大厅/科技房卡多少钱一张Sa9Ix苹果iPhone 17手机即将进...
一分钟实测分享”时光互娱低价获... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...
一分钟了解!牛牛房卡游戏平台加... 微信游戏中心:乐乐堂房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程序...
正规平台有哪些,金花房卡是正规... 海航大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
秒懂教程!微信牛牛房间怎么弄,... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:66336574许多玩家在游戏中会购买房卡来享受...
终于找到“微信怎样开炸金花房间... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
秒懂百科”海洋世界哪里有详细房... 哪里有详细房卡介绍是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:113857776许多玩家在游戏中...
头条推荐!游戏推荐斗牛房卡出售... 头条推荐!游戏推荐斗牛房卡出售新神兽/流樱大厅/微信链接房间卡怎么购买新神兽/流樱大厅是一款非常受欢...
推荐一款!怎么买斗牛房卡朱雀大... 微信游戏中心:朱雀大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
玩家攻略,牛牛房卡代理荣耀联盟... 荣耀联盟是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
IA解析/牛牛房卡游戏平台加盟... IA解析/牛牛房卡游戏平台加盟天道联盟/随意玩/微信链接房卡卖家联系方式Sa9Ix苹果iPhone ...
分享经验”百万牛哪里买低价获取... 分享经验”百万牛哪里买低价获取“新老夫子房卡充值游戏中心打开微信,添加客服【113857776】,进...
一分钟了解!金花房卡出售大众互... 今 日消息,大众互娱房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
秒懂教程!微信买链接拼三张房卡... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:56001354许多玩家在游戏中会购买房卡来享...
房卡必备教程“购买金花房卡联系... 新琉璃金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
1分秒分析”茄子娱乐房卡详细充... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...