鸿蒙系列--组件先容之根本组件

打印 上一主题 下一主题

主题 1010|帖子 1010|积分 3034

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、通用属性和文本样式 

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

二、Text

文本组件,可以包罗子组件Span
1.根本用法

  1. @Entry
  2. @Component
  3. struct TextPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         Text("默认样式Text组件")
  8.         Text("可设置基础文本样式")
  9.           .fontColor(Color.Red)
  10.           .fontStyle(FontStyle.Italic)
  11.           .fontSize(30)
  12.           .fontWeight(FontWeight.Bold)
  13.           .fontFamily("Arial,sans-serif")
  14.       }
  15.       .width('100%')
  16.     }
  17.     .height('100%')
  18.   }
  19. }
复制代码

2.常用属性

textAlign:

  1. @Entry
  2. @Component
  3. struct TextPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         Text("水平对齐尾部")
  8.           .width('100%')
  9.           .backgroundColor(Color.Yellow)
  10.           .textAlign(TextAlign.End)
  11.       }
  12.       .width('100%')
  13.     }
  14.     .height('100%')
  15.   }
  16. }
复制代码
        设置文本的对齐方式
参数类型为TextAlign,可选类型:


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

maxLines:

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

        设置文本截取方式
  1.     .textOverflow({overflow:TextOverflow.Ellipsis})
  2.     .maxLines(1)
复制代码
        当文本内容较多超出了组件范围时,用来设置文本截取方式,需配合maxLines使用,单独设置不见效
参数类型为TextOverflow,可设置类型:


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

decoration:

        设置文本装饰线样式及其颜色
  1. .decoration({ type: TextDecorationType.Underline, color: Color.Red })
复制代码
设置文本装饰线样式,使用TextDecorationType设置,可设置的类型:


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

三、Image

图片组件,可以给Image设置图片所在、宽和高
1.根本用法

  1. @Entry
  2. @Component
  3. struct ImagePage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         Image($r("app.media.icon"))
  8.           .width(100)
  9.           .height(100)
  10.       }
  11.       .width('100%')
  12.     }
  13.     .height('100%')
  14.   }
  15. }
复制代码

2.常用属性

objectFit

        设置图片的缩放类型
  1.     .objectFit(ImageFit.Contain)
复制代码

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


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

  1. Image('https://网络图片.jpg')
复制代码
        需要在module.json5文件中阐明网络访问权限
  1. {
  2.     "module" : {
  3.         "requestPermissions":[
  4.            {
  5.              "name": "ohos.permission.INTERNET"
  6.            }
  7.         ]
  8.     }
  9. }
复制代码
四、TextInput

输入单行文本
1.根本用法

  1. @Entry
  2. @Component
  3. struct TextInputPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         TextInput()
  8.           .fontColor(Color.Blue)
  9.           .fontSize(20)
  10.           .fontStyle(FontStyle.Italic)
  11.           .fontWeight(FontWeight.Bold)
  12.           .fontFamily('Arial')
  13.       }
  14.       .width('100%')
  15.     }
  16.     .height('100%')
  17.   }
  18. }
复制代码

2.设置提示文本:placeholder      

  1. @Entry
  2. @Component
  3. struct TextInputPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         TextInput({ placeholder: '请输入账号' })
  8.           .placeholderColor(0x999999)
  9.           .placeholderFont({ size: 20, weight: FontWeight.Medium, family: 'cursive', style: FontStyle.Italic })
  10.       }
  11.       .width('100%')
  12.     }
  13.     .height('100%')
  14.   }
  15. }
复制代码


  • placeholderColor:设置提示文本的颜色
  • placeholderFont:设置提示文本的样式,可以设置文本大小size,粗细weight,样式style,字体family
3.设置输入类型:type

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


  • Normal:根本输入模式。支持输入数字、字母、下划线、空格、特殊字符
  • Password:暗码输入模式
  • Email:e-mail所在输入模式
  • Number:纯数字输入模式

4.设置光标位置

        使用TextInputController的caretPosition方法
创建一个TextInputController对象,将这个对象设置到TextInput组件中,当点击按钮时,如果输入的字符数满足条件,光标会移动到caretPosition设置的position中
  1. @Entry
  2. @Component
  3. struct TextInputPage {
  4.   controller: TextInputController = new TextInputController()
  5.   build() {
  6.     Row() {
  7.       Column() {
  8.         TextInput({ controller: this.controller })
  9.           .type(InputType.Email)
  10.         Button('设置光标位置')
  11.           .onClick(() => {
  12.             this.controller.caretPosition(2)
  13.           })
  14.       }
  15.       .width('100%')
  16.     }
  17.     .height('100%')
  18.   }
  19. }
复制代码

5.获取输入文本

可以给TextInput设置onChange事件,输入文本发生变革时触发回调
  1. @Entry
  2. @Component
  3. struct TextInputPage {
  4.   @State text: string = ''
  5.   build() {
  6.     Row() {
  7.       Column() {
  8.         TextInput({ placeholder: '请输入账号' })
  9.           .onChange((value:string) => {
  10.             this.text = value
  11.           })
  12.         Text(this.text)
  13.       }
  14.       .width('100%')
  15.     }
  16.     .height('100%')
  17.   }
  18. }
复制代码


6.其他属性

caretColor:

修改光标颜色
  1.     .caretColor(Color.Red)
复制代码
五、Button

用来响应点击事件操纵,可以包罗子组件
1.根本用法

实现一个登录按钮
  1. @Entry
  2. @Component
  3. struct ButtonPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         Button('登录', { type: ButtonType.Capsule, stateEffect: true })
  8.           .width('90%')
  9.           .height(40)
  10.           .fontSize(16)
  11.           .fontWeight(FontWeight.Medium)
  12.           .backgroundColor('#007DFF')
  13.       }
  14.       .width('100%')
  15.     }
  16.     .height('100%')
  17.   }
  18. }
复制代码

2.设置按钮样式:type

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


  • Capsule:胶囊型按钮(圆角默认为高度的一半)
  • Circle:圆形按钮
  • Normal:普通按钮(默认不带圆角)
用stateEffect来设置按钮是否开启切换效果(闪动),false时,点击效果关闭,默认值为true。
  1.     Button('登录', { type: ButtonType.Capsule, stateEffect: true })
复制代码
3.设置按钮点击事件

用onClick来给按钮绑定点击事件
  1.       .onClick(()=>{
  2.             // 处理点击事件逻辑
  3.        })
复制代码
4.设置子组件

  1. @Entry
  2. @Component
  3. struct ButtonPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         Button({ type: ButtonType.Capsule, stateEffect: true }) {
  8.           Row() {
  9.             Image($r('app.media.icon'))
  10.               .width(30)
  11.               .height(30)
  12.             Text('登录')
  13.               .fontSize(20)
  14.               .fontColor(Color.White)
  15.           }
  16.         }
  17.         .width('90%')
  18.         .height(40)
  19.         .backgroundColor(Color.Red)
  20.       }
  21.       .width('100%')
  22.     }
  23.     .height('100%')
  24.   }
  25. }
复制代码

六、LoadingProgress

进度条组件
可以设置颜色color,和宽width、高height        
  1. @Entry
  2. @Component
  3. struct LoadingProgressPage {
  4.   build() {
  5.     Row() {
  6.       Column() {
  7.         LoadingProgress()
  8.           .color(Color.Blue)
  9.           .height(60)
  10.           .width(60)
  11.       }
  12.       .width('100%')
  13.     }
  14.     .height('100%')
  15.   }
  16. }
复制代码




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表