当前位置:   article > 正文

HarmonyOS开发(十一):ArkTS组件通用属性_arkts 设置空间宽度填满 左右留边距

arkts 设置空间宽度填满 左右留边距

1、组件通用属性

1.1、尺寸设置

用来设置组件的宽度、边距

相关属性

名称参数说明描术
widthLength设置组件的自身宽度,缺省的情况组件宽度为其内容的宽度
heightLength设置组件的自身高度,缺省的情况组件高度为其内容的度度
size

{

  width?:Length,

  height?:Length

}

设置组件的宽度尺寸
paddingPadding | Length

设置组件内边距,当参数为Length时,四个方向的内边距同时生效

默认值为0

marginMargin | Length

设置组件外边距,当参数为Length时,四个方向的内边距同时生效

默认值为0

constraintSize

{

  minWidth?:Length,

  maxWidth?:Length,

  minHeight?:Length,

  maxHeight?:Length

}

设置约束尺寸,组件布局时,进行尺寸范围限制

默认值:

{

  minWidth:0,

  maxWidth:Infinity,

  minHeight?:0,

  maxHeight?:Infinity

}

layoutWeightnumber | string

容器尺寸确定时,元素与兄弟元素主轴布局尺寸按权重进行分配,忽略本身尺寸设置,表示自己适应占满剩余空间。

默认值:0

注意:仅在Row/Column/Flex布局中生效

  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. // 列部局容器(从上到下,每个组件之间有10vp的间隙)
  6. Column({space: 10}) {
  7. Text('margin and padding').fontSize(12).fontColor('#36D').width('90%')
  8. Row() {
  9. Row() {
  10. // size设置组件的宽高
  11. Row().size({ width: '100%', height: '100%' }).backgroundColor(Color.Blue)
  12. }
  13. // 设置组件的宽(80vp),高(90vp),内边距(10),外边距(20),背景色为白色
  14. .width(80).height(90).padding(10).margin(20).backgroundColor(Color.White)
  15. }
  16. // 组件背景色为粉色
  17. .backgroundColor(Color.Pink)
  18. Text('constraintSize').fontSize(12).fontColor('#36D').width('90%')
  19. Text('这是一个长的字符串,这个字符串会非常的长,这是一个长的字符串,这个字符串会非常的长,这是一个长的字符串,这个字符串会非常的长,这是一个长的字符串,这个字符串会非常的长')
  20. .width('90%')
  21. // 约束组件最大宽度是200vp
  22. .constraintSize({maxWidth: 200})
  23. Text('layoutWeight').fontSize(12).fontColor('#36D').width('90%')
  24. // 父容器尺寸确定,设置了layoutWeight的子元素在主轴布局尺寸按权重进行分配,忽略本身尺寸设置
  25. Row() {
  26. Text('layoutWeight(1)')
  27. .size({ width: '30%', height: 100 }).backgroundColor(Color.Orange).textAlign(TextAlign.Center)
  28. // 权重为1,占主轴的1/3
  29. .layoutWeight(1)
  30. Text('layoutWeight(2)')
  31. .size({ width: '30%', height: 100 }).backgroundColor(Color.Red).textAlign(TextAlign.Center)
  32. // 权重为2,占主轴的2/3
  33. .layoutWeight(2)
  34. Text('no layoutWeight')
  35. .size({ width: '30%', height: 100 }).backgroundColor(Color.Gray).textAlign(TextAlign.Center)
  36. }
  37. .size({ width: '90%', height: 120 }).backgroundColor(Color.Yellow)
  38. }.width('100%').margin({ top: 5})
  39. }
  40. }

1.2、位置设置

用来设置组件的对齐方式、布局方向和显示位置

名称参数类型描述
alignAlignment

设置元素内容的对齐方式,只有当设置的width,height的大小超过元素本身内容时才生效

默认值:Alignment.Center

directionDirection

高置元素水平方向上的布局

默认值:Direction.Auto

position

{

  x:Length,

y:Length

}

使用绝对定位,设置元素锚点相对于父容器顶部起点的偏移位置

在布局容器中,设置这个属性不影响父容器布局,仅在绘制时做位置调整

markAnchor

{

  x:Length,

y:Length

}

设置元素在位置定位时的锚点,以元素顶部作为基点进行偏移。

默认值是:

{

  x: 0,

  y: 1

}

offset

{

  x:Length,

y:Length

}

相对布局完成位置坐标偏移量,设置这个属性,不影响父容器布局,仅在绘制时进行位置调整。

默认值:

{

  x: 0,

  y: 1

}

  1. @Entry
  2. @Component
  3. struct PositionTest {
  4. build() {
  5. Column() {
  6. Column({ space: 10 }) {
  7. Text('align').fontSize(12).fontColor('#36D').width('90%')
  8. Text('top start')
  9. // 设置元素内容的对齐方式
  10. .align(Alignment.TopStart)
  11. .height(50)
  12. .width('90%')
  13. .fontSize(16)
  14. .backgroundColor(Color.Pink)
  15. Text('direction').fontSize(12).fontColor('#36D').width('90%')
  16. Row() {
  17. Text('1').height(50).width('25%').maxFontSize(16).backgroundColor(Color.Pink).textAlign(TextAlign.Center)
  18. Text('2').height(50).width('25%').maxFontSize(16).backgroundColor(Color.Yellow).align(Alignment.TopStart)
  19. Text('3').height(50).width('25%').maxFontSize(16).backgroundColor(Color.Pink).align(Alignment.Top)
  20. Text('4').height(50).width('25%').maxFontSize(16).backgroundColor(Color.Yellow).align(Alignment.Bottom)
  21. }
  22. .width('90%')
  23. // 元素水平方向上的布局
  24. .direction(Direction.Rtl)
  25. }
  26. }
  27. .width('100%').margin({top: 5}).direction(Direction.Rtl)
  28. }
  29. }

  1. @Entry
  2. @Component
  3. struct PositionTest2 {
  4. build() {
  5. Column({space: 15}) {
  6. // 绝对定位,容器左上角相对于父组件左上角的偏移量
  7. Text('position').fontSize(12).fontColor('#36D').width('90%')
  8. Row() {
  9. Text('1').size({width:'30%',height:'50'}).backgroundColor('#78a355').border({width:1}).fontSize(16)
  10. Text('2 position(30,10)')
  11. .size({width: '60%',height:'30'})
  12. .backgroundColor('#b22c46')
  13. .border({width:1})
  14. .fontSize(16)
  15. .align(Alignment.Start)
  16. .position({x:30,y:10})
  17. Text('3').size({width:'45%',height:'50'}).backgroundColor('#007d65').border({width:1}).fontSize(16)
  18. Text('4 position(50%,70%)')
  19. .size({width:'50%',height:'50'})
  20. .backgroundColor('#c37e00')
  21. .border({width:1})
  22. .fontSize(16)
  23. .position({x:'50%',y:'70%'})
  24. }
  25. .width('90%').height(100).border({width:1,style:BorderStyle.Dashed})
  26. // 相对于起点偏移,其中x为最终定位点距离起点水平方向间距,x>0则向左,反之则向右
  27. // y为最终定位点距离起点垂直方向间距,y>0则向上,反之则向下
  28. Text('markAnchor').fontSize(12).fontColor('#36D').width('90%')
  29. Stack({alignContent: Alignment.TopStart}) {
  30. Row()
  31. .size({width: '100', height: '100'})
  32. .backgroundColor('#f2eada')
  33. Text('text')
  34. .size({width: 25, height: 25})
  35. .backgroundColor('#f15a22')
  36. .markAnchor({x: 25,y: 25})
  37. Text('text')
  38. .size({width: 25, height: 25})
  39. .backgroundColor('#f15a22')
  40. .markAnchor({x: -100,y: -25})
  41. Text('text')
  42. .size({width: 25, height: 25})
  43. .backgroundColor('#f15a22')
  44. .markAnchor({x: 25,y: -25})
  45. }
  46. .margin({top: 25}).border({width:1,style: BorderStyle.Dashed})
  47. // 相对定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上
  48. Text('offset').fontColor('#36D').fontSize(12).width('90%')
  49. Row() {
  50. Text('1').size({width:'15%',height:'50'}).backgroundColor('#fedcbd').border({width:1}).fontSize(16)
  51. Text('2 offset(15,30)')
  52. .size({width:120,height:'50'})
  53. .backgroundColor('#4e72b8')
  54. .border({width: 1})
  55. .fontSize(16)
  56. .align(Alignment.Start)
  57. .offset({x:15,y:30})
  58. Text('3').size({width:'15%',height:'50'}).backgroundColor('#fedcbd').border({width:1}).fontSize(16)
  59. Text('4 offset(-5%,20%)')
  60. .size({width:100,height:'50'})
  61. .backgroundColor('#4e72b8')
  62. .border({width:1})
  63. .fontSize(16)
  64. .offset({x: '-5%',y:'20%'})
  65. }.width('90%').height(100).border({width:1,style:BorderStyle.Dashed})
  66. }
  67. .width('100%').margin({top: 25})
  68. }
  69. }

1.3、布局约束

通过组件的宽高比和显示优先级约束组件显示效果。

名称参数说明描述
aspectRationumber指定当前组件的宽高比
displayPrioritynumber

设置当前组件在布局容器中显示的优先级

当父容器的空间不足时,低优先级的组件会隐藏

注意:仅在Row/Column/Flex(单行)容器组件中生效

  1. @Entry
  2. @Component
  3. struct AspectRatioTest {
  4. private children: string[] = ['1','2','3','4','5','6']
  5. build() {
  6. Column({space: 20}) {
  7. Text('using container: row').fontSize(12).fontColor('#36D').width('100%')
  8. Row({space: 10}) {
  9. ForEach(this.children,(item) => {
  10. Text(item)
  11. .backgroundColor('#deab8a')
  12. .fontSize(16)
  13. // 指定组件的宽高比是1.5
  14. .aspectRatio(1.5)
  15. .height(60)
  16. Text(item)
  17. .backgroundColor('#deab8a')
  18. .fontSize(16)
  19. // 指定组件宽高比是1.5
  20. .aspectRatio(1.5)
  21. .width(60)
  22. },item => item)
  23. }
  24. .size({width: '100%',height: 100})
  25. .backgroundColor('#feeeed')
  26. .clip(true)
  27. Text('using container: grid').fontSize(12).fontColor('#36D').width('100%')
  28. Grid() {
  29. ForEach(this.children,(item) => {
  30. GridItem(){
  31. Text(item)
  32. .backgroundColor('#deab8a')
  33. .fontSize(16)
  34. .width(60)
  35. .aspectRatio(1.5)
  36. }
  37. },item => item)
  38. }
  39. .columnsTemplate('1fr 1fr 1fr')
  40. .columnsGap(10)
  41. .rowsGap(10)
  42. .size({width:'100%',height:165})
  43. .backgroundColor('#feeeed')
  44. }.padding(10)
  45. }
  46. }
  1. // ../model/ContainerInfo.ts
  2. export class ContainerInfo {
  3. label: string = ''
  4. size: string = ''
  5. }
  6. export class ChildInfo {
  7. text: string = ''
  8. priority: number = 0
  9. }
  1. import {ContainerInfo,ChildInfo} from '../model/ContainerInfo'
  2. @Entry
  3. @Component
  4. struct DisplayPriorityTest {
  5. private container: ContainerInfo[] = [
  6. {label: 'Big container',size: '90%'},
  7. {label: 'Middle container',size: '50%'},
  8. {label: 'Small container',size: '30%'}
  9. ];
  10. private children: ChildInfo[] = [
  11. {text: '1\n(priority:2)', priority: 2},
  12. {text: '2\n(priority:1)', priority: 1},
  13. {text: '3\n(priority:3)', priority: 3},
  14. {text: '4\n(priority:1)', priority: 1},
  15. {text: '5\n(priority:2)', priority: 2}
  16. ]
  17. @State currentIndex: number = 0
  18. build() {
  19. Column({ space: 10 }) {
  20. Button(this.container[this.currentIndex].label).backgroundColor('#5c7a29')
  21. .onClick(() => {
  22. this.currentIndex = (this.currentIndex + 1) % this.container.length;
  23. })
  24. Flex({justifyContent: FlexAlign.SpaceBetween}) {
  25. ForEach(this.children, (item:ChildInfo) => {
  26. Text(item.text)
  27. .width(120)
  28. .height(60)
  29. .fontSize(24)
  30. .textAlign(TextAlign.Center)
  31. .backgroundColor('#b22c46')
  32. // 设置组件显示的优先级
  33. .displayPriority(item.priority)
  34. },item => item)
  35. }
  36. .width(this.container[this.currentIndex].size)
  37. .backgroundColor('#d3d7d4')
  38. }
  39. .width('100%')
  40. .margin({top:50})
  41. }
  42. }

1.4、Flex布局

名称参数说明描述
flexBasisstring | number

设置组件在父亲容器主轴方向上的基准尺寸

默认值:‘auto’,表示组件在主轴方向上的基准尺寸为组件原大小

flexGrownumber

设置父容器的剩余空间分配给此属性所在组件的比例

默认值:0

flexShrinknumber

设置父容器压缩尺寸分配给此属性所在组件的比例

默认值:1

alignSelfItemAlign

子组件在父容器交叉轴的对齐格式,覆盖Flex布局容器中alignItems默认配置

默认值:ItemAlign.Auto

  1. @Entry
  2. @Component
  3. struct FlexTest1 {
  4. build() {
  5. Column({space:5}) {
  6. Text('flexBasis').fontSize(12).fontColor('#36D').width('90%')
  7. // 基于主轴的基准尺寸
  8. Flex() {
  9. Text('flexBasis(100)')
  10. .flexBasis(100) // 宽度为100vp
  11. .height(100)
  12. .backgroundColor('#f391a9')
  13. .textAlign(TextAlign.Center)
  14. Text('flexBasis(auto)')
  15. .flexBasis('auto') // 保存原本宽度的60%
  16. .width('60%')
  17. .height(100)
  18. .backgroundColor('#8f4b2e')
  19. .textAlign(TextAlign.Center)
  20. }
  21. .width('90%').height(120).padding(10).backgroundColor('#deab8a')
  22. Text('flexShrink').fontSize(12).fontColor('#36D').width('90%')
  23. // flexShrink()表示该元素的压缩比例,它对超出的总尺寸进行计算
  24. // 压缩比例为0则不压缩
  25. Flex({direction:FlexDirection.Row}) {
  26. Text('flexShrink(0)')
  27. .flexShrink(0)
  28. .width('50%')
  29. .height(100)
  30. .backgroundColor('#f391a9')
  31. .textAlign(TextAlign.Center)
  32. Text('default flexShrink') // 默认情况下 flexShrink的值值是1
  33. .width('40%')
  34. .height(100)
  35. .backgroundColor('#f391a9')
  36. .textAlign(TextAlign.Center)
  37. Text('flexShrink(1)')
  38. .flexShrink(1)
  39. .width('40%')
  40. .height(100)
  41. .backgroundColor('#f391a9')
  42. .textAlign(TextAlign.Center)
  43. }
  44. .width('90%').height(120).padding(10).backgroundColor('#deab8a')
  45. Text('alignSelf').fontSize(12).fontColor('#36D').width('90%')
  46. // alignSelf会覆盖掉Flex布局容器中的alignItems设置
  47. Flex({direction: FlexDirection.Row,alignItems: ItemAlign.Center}) {
  48. Text('no alignSelf,height:70')
  49. .width('33%')
  50. .height(70)
  51. .backgroundColor('#f391a9')
  52. .textAlign(TextAlign.Center)
  53. Text('alignSelf End')
  54. .alignSelf(ItemAlign.End)
  55. .width('33%')
  56. .height(70)
  57. .backgroundColor('#f391a9')
  58. .textAlign(TextAlign.Center)
  59. Text('no alignSelf,height:100%')
  60. .width('34%')
  61. .height('100%')
  62. .backgroundColor('#f391a9')
  63. .textAlign(TextAlign.Center)
  64. }
  65. .width('90%').height(120).padding(10).backgroundColor('#deab8a')
  66. }
  67. .width('100%')
  68. .margin({top: 5})
  69. }
  70. }

1.5、边框设置

属性

名称参数类型描述
border

{

  width?:Length | EdgeWidths,

  color?:ResourceColor | EdgeColors,

  radius?:Length | BorderRadiuses,

  style?:BorderStyle | EdgeStyles

}

统一边框样式设置

width:边框的宽度

color:边框的颜色

radius:边框圆角半径

style:设置边框样式

边框宽度默认值为0,也就是不显边框

borderStyleBorderStyle | EdgeStyles

设置边框样式

默认值:BorderStyle.Solid

注意:api 9后支持ArkTS卡片中使用,但只支持设置相同的边框样式

borderWidthLength | EdgeWidths

设置边框的宽度,不支持百分比

注意:api 9后支持ArkTS卡片中使用,但只支持设置相同的边框宽度

borderColorResourceColor | EdgeColors

设置边框颜色

默认值:Color.Black

注意:api 9后支持ArkTS卡片中使用,但只支持设置相同的边框颜色

borderRadiusLength | BorderRadiuses

设置边框的圆角半径,不支持百分比

注意:api 9后支持ArkTS卡片中使用,但只支持设置相同的边框圆角半径

EdgeWidths

api 9中支持的,引入这个对象时,至少要传入一个参数参

其相关的参数如下:

left:Length                左边框的宽度

right:Length                右边框的宽度

top:Length                上边框的宽度

bottom:Length                下边框宽度

EdgeColors

api 9中支持的,引入这个对象时,至少要传入一个参数参

其相关的参数如下:

left:ResourceColor                左边框的颜色

right:ResourceColor                右边框的颜色

top:ResourceColor                上边框的颜色

bottom:ResourceColor                下边框颜色

EdgeRadiuses

api 9中支持的,引入这个对象时,至少要传入一个参数参

其相关的参数如下:

topLeft:Length                左上角圆角半径

topRight:Length                右上角圆角半径

bottomLeft:Length                左下角圆角半径

bottomRight:Length                右下角圆角半径

EdgeStyles

api 9中支持的,引入这个对象时,至少要传入一个参数参

其相关的参数如下:

left:BorderStyle                左边框的样式

right:BorderStyle                右边框的样式

top:BorderStyle                上边框的样式

bottom:BorderStyle                下边框样式

  1. @Entry
  2. @Component
  3. struct BorderTest {
  4. build() {
  5. Column({space: 10}) {
  6. Flex({justifyContent: FlexAlign.SpaceAround,alignItems:ItemAlign.Center}) {
  7. Text('dashed')
  8. .borderStyle(BorderStyle.Dashed).borderWidth(5).borderColor('#003a6c').borderRadius(10)
  9. .width(120).height(120).textAlign(TextAlign.Center).fontSize(16)
  10. Text('dotted')
  11. .border({width: 5, color: '#769149', radius: 10, style: BorderStyle.Dotted})
  12. .width(120).height(120).textAlign(TextAlign.Center).fontSize(16)
  13. }.width('100%').height(150)
  14. Text('.border')
  15. .fontSize(30)
  16. .width(300)
  17. .height(300)
  18. .border({
  19. width: {left: '50px',right: '100px',top: '200px',bottom: '300px'},
  20. color: {left: '#1a2933', right: '#401c44', top: Color.Pink, bottom: Color.Orange},
  21. radius: {
  22. topLeft: 10,
  23. topRight: 20,
  24. bottomLeft:40,
  25. bottomRight: 60
  26. },
  27. style: {
  28. left: BorderStyle.Dotted,
  29. right: BorderStyle.Dotted,
  30. top: BorderStyle.Solid,
  31. bottom: BorderStyle.Dashed
  32. }
  33. }).textAlign(TextAlign.Center)
  34. }
  35. }
  36. }

 1.6、图片边框设置

名称参数类型描述
borderImageBorderImageOption

图片边框或渐变色边框

支持在卡片中使用

BorderImageOption

 souurce:string|Resource|linearGradient      边框图片源或渐变色设置

slice:Length|EdgeWidths          设置图片边框的切割宽度,默认值为0,当设置为负数时直接取默认值,当设置为Length时,则统一设置四个角的宽度。如果使用EdgeWidths则可以分别设置Top,Bottom,Left,Right

width:Length|EdgeWidths        设置图片边框的宽度,默认值为0,当设置为负数时直接取默认值,当设置为Length时,则统一设置四边的宽度。如果使用EdgeWidths则可以分别设置Top,Bottom,Left,Right

outset:Length|EdgeWidths        设置边框图片向外延生距离,默认值是0,当设置为负数时直接取默认值,当设置为Length时,则统一设置四边的向外延生距离。如果使用EdgeWidths则可以分别设置Top,Bottom,Left,Right
repeat:RepeatMode        设置边框图片的重复方式,默认值:RepeatMode.Stretch

fill:boolean        设置边框图片中心填充,默认值:false

RepeatMode枚举说明:

Repeat                被切割图片重复平铺在图片的边框上,超出部分会被裁剪

Stretch                被切割图片以拉伸填充的方式铺满图片边框

Round                被切割图片以整数次平铺在图片边框上,无法以整数次平铺时压缩被切割图片

Space                被切割图片以整数次平铺在图片边框上,无法以整数次平铺时以空白填充

  1. @Entry
  2. @Component
  3. struct BorderImageTest2 {
  4. @State WidthValue: number = 0;
  5. @State SliceValue: number = 0;
  6. @State OutSetValue: number = 0;
  7. @State RepeatValue: RepeatMode[] = [RepeatMode.Repeat, RepeatMode.Stretch,RepeatMode.Round,RepeatMode.Space];
  8. @State SelectIndex: number = 0;
  9. @State SelectText: string = 'Repeat';
  10. @State FillValue: boolean = false;
  11. build() {
  12. Row() {
  13. Column({ space: 20 }) {
  14. Row() {
  15. Text('borderImage').textAlign(TextAlign.Center).fontSize(50)
  16. }
  17. .borderImage({
  18. source: $r('app.media.my_border'), // 边框图片源或渐变色设置
  19. slice: this.SliceValue, // 设置图片边框的切割宽度
  20. width: this.WidthValue, // 设置图片边框的宽度
  21. outset: this.RepeatValue[this.SelectIndex], // 设置边框图片向外延生距离
  22. repeat: this.RepeatValue[this.SelectIndex], // 设置边框图片的重复方式
  23. fill: this.FillValue // 设置边框图片中心填充
  24. })
  25. Column() {
  26. Text(`borderImageSlice = ${this.SliceValue}px`)
  27. Slider({
  28. value: this.SliceValue,
  29. min: 0,
  30. max: 100,
  31. style: SliderStyle.OutSet
  32. })
  33. .onChange((value:number,mode:SliderChangeMode) => {
  34. this.SliceValue = value;
  35. })
  36. }
  37. Column() {
  38. Text(`borderImageWidth = ${this.WidthValue}}px`)
  39. Slider({
  40. value: this.WidthValue,
  41. min:0,
  42. max:100,
  43. style:SliderStyle.OutSet
  44. })
  45. .onChange((value:number,mode:SliderChangeMode) => {
  46. this.WidthValue = value;
  47. })
  48. }
  49. Column() {
  50. Text(`borderImageOutSet = ${this.OutSetValue}px`)
  51. Slider({
  52. value: this.OutSetValue,
  53. min:0,
  54. max:100,
  55. style:SliderStyle.OutSet
  56. })
  57. .onChange((value:number,mode:SliderChangeMode) => {
  58. this.OutSetValue = value;
  59. })
  60. }
  61. Row() {
  62. Text('borderImageRepeat:')
  63. Select([{value:'Repeat'},{value:'Stretch'},{value:'Round'},{value:'Space'}])
  64. .value(this.SelectText)
  65. .onSelect((index:number,text:string) => {
  66. this.SelectIndex = index;
  67. this.SelectText = text;
  68. })
  69. }
  70. Row() {
  71. Text(`borderImageFill: ${this.FillValue}`)
  72. Toggle({type:ToggleType.Switch,isOn: this.FillValue})
  73. .onChange((isOn:boolean) => {
  74. this.FillValue = isOn;
  75. })
  76. }
  77. }
  78. .width('100%')
  79. }
  80. .height('100%')
  81. }
  82. }

1.7、背景设置

用来设置组件的背景样式

名称参数类型描述
backgroundColorResourceColor

设置组件的背景色

支持在卡片中使用

backgroundImage

src:ResourceStr,

repeat?:ImageRepeat

src:图片地址,支持网络图片资源和本地图片(不支持svg类型的图片)

repeat:设置背景图片的重复样式,默认不重复。当设置的背景图片为透明底色图片,且同时设置了backgroundColor时,二者叠加显示,背景颜色在最底部。

支持在卡片中使用

backgroundImageSize

{

  width?:Length,

  height?:Length

} | ImageSize

设置背景图像的高度和宽度。当输入为{width:Length,height:Length}对象时,如果只设置一个属性 ,则第二个属性保持图片原始宽度比进行调整

width与height取值范围:[0,+∞]

默认值:ImageSize.Auto

支持在卡片中使用

设置为小于0时,按值为0显示

backgroundImagePositionPosition | Alignment

设置背景图在组件中显示位置,相对于组件左上角的坐标

默认值:

{

  x: 0,

  y: 0

}

x和y值设置百分比时,偏移量是相对组件自身宽高计算的

支持在卡片中使用

  1. @Entry
  2. @Component
  3. struct BackgroundTest {
  4. build() {
  5. Column({space:5}) {
  6. Text('background color').tipStyle()
  7. // backgroundColor,设置背景颜色
  8. Row().width('90%').height(50).backgroundColor('#c77eb5').border({width: 1})
  9. Text('background image repeat along X').tipStyle()
  10. Row()
  11. // 设置背景图片
  12. .backgroundImage($r('app.media.my_border'), ImageRepeat.X)
  13. // 设置背景图像的高度和宽度
  14. .backgroundImageSize({width: '250px',height:'140px'})
  15. .width('90%')
  16. .height(70)
  17. .border({width: 1})
  18. Text('background image repeat along Y').tipStyle()
  19. Row()
  20. .backgroundImage($r('app.media.my_border'), ImageRepeat.Y)
  21. .backgroundImageSize({width: '500px',height:'120px'})
  22. .width('90%')
  23. .height(100)
  24. .border({width:1})
  25. Text('background image size').tipStyle()
  26. Row()
  27. .width('90%').height(150)
  28. .backgroundImage($r('app.media.my_border'), ImageRepeat.NoRepeat)
  29. .backgroundImageSize({width: 1000,height:500})
  30. .border({width: 1})
  31. Text('background fill the box(Cover)').tipStyle()
  32. // 不保证图片完整的情况下占满盒子
  33. Row()
  34. .width(200)
  35. .height(50)
  36. .backgroundImage($r('app.media.my_border'), ImageRepeat.NoRepeat)
  37. .backgroundImageSize(ImageSize.Cover)
  38. .border({width: 1})
  39. Text('background fill the box(Contain)').tipStyle()
  40. // 保证图片完整的情况下放到最大
  41. Row()
  42. .width(200)
  43. .height(50)
  44. .backgroundImage($r('app.media.my_border'), ImageRepeat.NoRepeat)
  45. .backgroundImageSize(ImageSize.Contain)
  46. .border({width: 1})
  47. Text('background image position').tipStyle()
  48. Row()
  49. .width(100)
  50. .height(50)
  51. .backgroundImage($r('app.media.my_border'), ImageRepeat.NoRepeat)
  52. .backgroundImageSize({width:1000,height:560})
  53. // 设置背景图在组件中显示位置,相对于组件左上角的坐标
  54. .backgroundImagePosition({x: -500, y: -300})
  55. .border({width: 1})
  56. }
  57. .width('100%').height('100%').padding({top: 5})
  58. }
  59. }
  60. @Extend(Text) function tipStyle() {
  61. .fontSize(12).width('90%').fontColor('#36D')
  62. }

 1.8、透明度设置

用来设置组件的透明度

名称参数类型描述
optacitynumber | Resource

元素的透明度配置

取值范围:[0,1]

1表示不透明,0表示完全透明(看不到组件,但是在布局中占位)

默认值:1

支持卡片中使用

注意:子组件可以继承父组件此属性

  1. @Extend(Text) function tipStyle1() {
  2. .fontSize(12).width('90%').fontColor('#36D')
  3. }
  4. @Extend(Text) function opacityStyle(opacity:number) {
  5. .width('90%').height(50).opacity(opacity).backgroundColor('#005344')
  6. }
  7. @Entry
  8. @Component
  9. struct OpacityTest {
  10. build() {
  11. Column({space: 5}) {
  12. // @ts-ignore
  13. Text('opacity(1)').tipStyle1()
  14. Text().opacityStyle(1)
  15. Text('opacity(0.7)').tipStyle1()
  16. Text().opacityStyle(0.7)
  17. Text('opacity(0.4)').tipStyle1()
  18. Text().opacityStyle(0.4)
  19. Text('opacity(0.1)').tipStyle1()
  20. Text().opacityStyle(0.1)
  21. Text('opacity(0)').tipStyle1()
  22. Text().opacityStyle(0)
  23. }
  24. .width('100%')
  25. .padding({top:5})
  26. }
  27. }

1.9、显隐控制

控制组件是否可见

名称参数类型描述
visibilityVisibility

控制当前组件显示或隐藏

默认值:Visibility.Visible

支持卡片中使用

注意:根据场景不同可以使用条件渲染来代替

  1. @Entry
  2. @Component
  3. struct VisibilityTest {
  4. build() {
  5. Column() {
  6. Column() {
  7. // 隐藏组件不参与占位
  8. Text('None').fontSize(12).width('90%').fontColor('#36D')
  9. Row().visibility(Visibility.None).width('90%').height(80).backgroundColor('#008792')
  10. // 隐藏组件参与占位
  11. Text('Hidden').fontSize(12).width('90%').fontColor('#36D')
  12. Row().visibility(Visibility.Hidden).width('90%').height(80).backgroundColor('#008792')
  13. // 显示组件,默认情况下就是显示模式
  14. Text('Visible').fontSize(12).width('90%').fontColor('#36D')
  15. Row().visibility(Visibility.Visible).width('90%').height(80).backgroundColor('#008792')
  16. }
  17. .width('90%').border({width: 1})
  18. }
  19. .width('100%').margin({top: 5})
  20. }
  21. }

1.10、禁用控制

控制组件是否可交互,可交互状态下可以响应点击事件、触摸事件、拖拽事件、按键事件、鼠标事件

名称参数类型描述
enableboolean

为true则可交互为false不可交互

默认值:true

支持在卡片中使用

  1. import Prompt from '@system.prompt'
  2. @Entry
  3. @Component
  4. struct EnableTest {
  5. @State isEnable: boolean = false
  6. build() {
  7. Flex({justifyContent: FlexAlign.SpaceAround}){
  8. Button('点击我').enabled(this.isEnable).backgroundColor('#78a355').opacity(0.8)
  9. .onClick(() => {
  10. Prompt.showToast({
  11. message:'你点击了按钮',
  12. duration:2000
  13. })
  14. })
  15. Row(){
  16. Text('切换状态:').fontSize(12).fontColor('#36D').lineHeight(20)
  17. Toggle({type: ToggleType.Switch,isOn: this.isEnable})
  18. .onChange((isOn:boolean) => {
  19. this.isEnable = isOn;
  20. })
  21. }
  22. }
  23. .width('100%')
  24. .padding({top: 5})
  25. }
  26. }

 1.11、浮层

用来设置组件的遮罩文本

名称参数类型描述
overlay

value:string,

options?:{

  align?:Alignment,

  offset?:{x?:number,y?:number}

}

在当前组件上设置遮罩文本

value:文本内容

options:文本定位

默认值:

{

  align:Alignment.TopStart,

  offset:{x:0,y:0}

}

支持在卡片中使用

  1. @Entry
  2. @Component
  3. struct OverlayTest {
  4. build() {
  5. Column() {
  6. Column(){
  7. Text('floating layer')
  8. .fontSize(12).fontColor('#36D').maxLines(1)
  9. Column() {
  10. Image($r('app.media.my_border'))
  11. .width(240).height(240)
  12. .overlay('这里的文本是图片组件上的遮罩文本',{
  13. align: Alignment.Bottom,
  14. offset: {x: 0, y: -15}
  15. })
  16. }
  17. .border({width:2,color:Color.Black})
  18. }.width('100%')
  19. }.padding({top: 15})
  20. }
  21. }

1.12、Z序控制

用来控制组件的堆叠循序

名称参数类型描述
zIndexnumber

同一容器中兄弟组件显示层级关系

zIndex值越大,显示的层级越高

支持在卡片中使用

  1. @Entry
  2. @Component
  3. struct ZIndexTest {
  4. build() {
  5. Column(){
  6. Stack() {
  7. // stack会重叠组件,默认后定义的在最上面
  8. // 具有较高zIndex值的元素会在zIndex较小的元素前面
  9. Text('1, zIndex(2)')
  10. .size({width: '40%', height: '30%'}).backgroundColor('#b3424a')
  11. .textAlign(TextAlign.Center)
  12. .zIndex(2)
  13. Text('2, default zIndex(1)')
  14. .size({width: '70%', height: '50%'}).backgroundColor('#6d8346')
  15. .align(Alignment.TopStart)
  16. .zIndex(1)
  17. Text('3, zIndex(0)')
  18. .size({width: '90%', height: '80%'}).backgroundColor('#867892')
  19. .align(Alignment.TopStart)
  20. }.width('100%').height(200)
  21. }.width('100%').height(200)
  22. }
  23. }

 1.13、图形变换

用于对组件进行旋转、平移、缩放、矩阵变换等操作。

名称参数说明描述
rotate

{

  x?:number,

  y?:number,

  z?:number,

  angle:number | string,

  centerX?:number | string,

  centerY?:number | string

}

可使组件在以组件左上角为坐标原点坐标系进行旋转

其中x,y,z指定一个矢量,作为旋转轴

angle:指定旋转的角度,当为负值时表示相对于旋转轴逆时针旋转

centerX,centerY用于指定旋转的中心点

默认值:

{

  x:0,

  y:0,

  z:0,

  centerX:'50%',

  centerY:'50%'

}

支持在卡片中使用

translate

{

  x?:number | string,

  y?:number | string,

  z?:number | string

}

可使组件在以组件左上角为坐标原点的坐标系中进行移动,x,y,z的值分别表示在对应轴上移动的距离

值为正时表示向对应轴的正向移动,值为负时表示向对应轴的反向移动

支持在卡片中使用

scale

{

  x?:number,

  y?:number,

  z?:number,

  centerX?:number | string,

  centerY?:number | string

}

可以分别设置X轴、Y轴、Z轴的缩放比例,默认值是1

同时可以通过centerX和centerY设置缩放中心点

默认值:

{

  x:0,

  y:0,

  z:0,

  centerX:'50%',

  centerY:'50%'

}

支持卡片中使用

transformMatrix4Transit设置当前组件的变换矩阵

  1. import matrix4 from '@ohos.matrix4'
  2. @Extend(Text) function textStyleExtend() {
  3. .fontColor('#36D').padding(15).fontSize(12)
  4. }
  5. @Extend(Row) function rowStyleExtend(color:ResourceStr) {
  6. .width(100).height(100).backgroundColor(color)
  7. }
  8. @Entry
  9. @Component
  10. struct TransformTest {
  11. build() {
  12. Column(){
  13. Text('rotate').width('90%').textStyleExtend()
  14. Row()
  15. // 组件以矢量(0,0,1)为旋转轴,绕中心点顺时针旋转300度
  16. .rotate({
  17. x: 0,
  18. y: 0,
  19. z: 1,
  20. centerX: '50%',
  21. centerY: '50%',
  22. angle: 300
  23. })
  24. .rowStyleExtend('#ef5b9c')
  25. Text('translate').textStyleExtend()
  26. Row()
  27. // x轴方向平移100,y轴方向平移10
  28. .translate({x: 100,y:10})
  29. .rowStyleExtend('#f47920')
  30. Text('scale').textStyleExtend()
  31. Row()
  32. // 高度缩小一倍,宽度放大一倍,z轴在2D下看不到效果的
  33. .scale({x:2,y:0.5})
  34. .rowStyleExtend('#87843b')
  35. Text('Matrix4').textStyleExtend()
  36. Row()
  37. .rowStyleExtend('#003a6c')
  38. .transform(matrix4.identity().translate({x: 50, y: 50}).scale({x: 1.5,y:1}).rotate({
  39. x:0,
  40. y:0,
  41. z:1,
  42. angle:60
  43. }))
  44. }
  45. .width('100%').margin({top: 5})
  46. }
  47. }

1.14、图像效果

设置组件的模糊、阴影效果以及设置图片的图像效果

名称参数类型描述
blurnumber

为当前组件添加内容模糊效果,入参为模糊半径,模糊半径越大越模糊,为0时不模糊

取值范围:[0,+∞)

支持卡片中使用

backdropBlurnumber

为当前组件添加背景模糊效果,入参为模糊半径,模糊半径越大越模糊,为0时不模糊。

取值范围:[0,+∞)

支持卡片中使用

shadow

{

  radius:number|Resource,

  color?:Color|string|Resource,

  offesetX?:number | Resource,

  offsetY?:number | Resource

}

为当前组件添加阴影效果。

radius为模糊半径,必填

color为阴影的颜色,默认是灰色

offsetX为x轴的偏移量,默认是0

offsetY为y轴的偏移量,默认是0

偏移量的单位是px

支持在卡片中使用

grayscalenumber

为当前组件添加灰度效果,值定义为灰度转换的比例

默认值:0.0

取值范围:[0,1]

入参1.0则完全转为灰度图像,入参则0.0图像无变化,入参在0.0和1.0之间时,效果呈线性变化

设置小于0的值时,按值为0处理,设置大于1的值时,按值为1处理。

支持在卡片中使用

brightnessnumber

为当前组件添加高光效果,入参为高光比例

值为1时无效果,小于1则变暗,0为全黑,大于1则亮度增加

取值范围:[0,+∞]

默认值:1.0

支持在卡片中使用

saturatenumber

为当前组件添加饱和度效果(饱和度为颜色中的含色成分和消色成分(灰)的比例)

入参为1显示原图

大于1含色成分越高,饱和度越高

小于1消色成分越高,饱和度越低

默认值:1.0

支持在卡片中使用

contrastnumber

为当前组件添加对比度效果

入参数为对比度的值

当值为1时显示原图,值越大对比度越高

当值为0时显示全灰色

取值范围:[0,+∞)

默认值:1.0

当设置小于0的值时,按值为0处理

支持卡片中使用

invertnumber

反转输入图像

入参为图像反转比例

值为1时完全反转,为0时图像无变化

默认值:0

设置值小于0时按0处理

支持在卡片中使用

sepianumber

把图像转为深褐色

入参为图像反转的比例

值为1则完全是深褐色的,值为0图像无变化

默认值:0

支持在卡片中使用

hueRotatenumber | string

色相旋转效果

入参为旋转角度

取值范围:(-∞, +∞)

默认值:0

支持在卡片中使用

colorBlendColor | stirng | Resorce

为当前组件添加颜色叠加效果

入参为叠加的颜色

支持在卡片中使用

字体背景模糊:

  1. @Entry
  2. @Component
  3. struct ImageEffectsTest1 {
  4. build() {
  5. Column({space: 10}) {
  6. // 模糊字体
  7. Text('字体模糊').fontSize(12).fontColor('#36D').width('90%')
  8. Flex({alignItems: ItemAlign.Center}) {
  9. Text('正常字休').margin(10)
  10. Text('模糊级别1')
  11. .blur(1).margin(10)
  12. Text('模糊级别2')
  13. .blur(2).margin(10)
  14. Text('模糊级别3')
  15. .blur(3).margin(10)
  16. }
  17. .width('90%').height(40)
  18. .backgroundColor('#feeeed')
  19. // 背景模糊
  20. Text('背景模糊').fontSize(12).fontColor('#36D').width('90%')
  21. Text()
  22. .width('90%')
  23. .height(40)
  24. .backdropBlur(5)
  25. .backgroundImage($r('app.media.my_border'))
  26. .backgroundImageSize({width: 1200,height: 160})
  27. }
  28. .width('100%').margin({top: 5})
  29. }
  30. }

图像效果:

  1. @Entry
  2. @Component
  3. struct ImageEffectsTest2 {
  4. build() {
  5. List({space: 10}) {
  6. ListItem(){
  7. Column(){
  8. Text('阴影效果').fontSize(12).fontColor('#36D').width('90%')
  9. Image($r('app.media.my_border'))
  10. .width('90%')
  11. .height(60)
  12. .shadow({ radius: 10,
  13. color: '#afb4db',
  14. offsetX: 20,
  15. offsetY: 30 })
  16. }.width('100%')
  17. }
  18. ListItem(){
  19. Column({space:5}){
  20. // 灰度设置为0~1,当为1时则完全灰度了
  21. Text('灰度效果').fontSize(12).fontColor('#36D').width('90%')
  22. Image($r('app.media.my_border')).width('90%').height(40).grayscale(0.8)
  23. Image($r('app.media.my_border')).width('90%').height(40).grayscale(0.4)
  24. }.width('100%')
  25. }
  26. ListItem(){
  27. Column({space:5}){
  28. // 高光效果,1为正常图片,小于1则变暗,大于1则变亮
  29. Text('高光效果').fontSize(12).fontColor('#36D').width('90%')
  30. Image($r('app.media.my_border')).width('90%').height(40).brightness(1.3)
  31. }
  32. .width('100%')
  33. }
  34. ListItem(){
  35. Column({space:5}){
  36. // 饱和度,原图是1,大于1含色成分越高,饱和度越高;小于1消色成分越高,饱和度越低
  37. Text('饱和度').fontSize(12).fontColor('#36D').width('90%')
  38. Image($r('app.media.my_border')).width('90%').height(40).saturate(2.0)
  39. Image($r('app.media.my_border')).width('90%').height(40).saturate(0.5)
  40. }
  41. .width('100%')
  42. }
  43. ListItem(){
  44. Column({space:5}){
  45. // 对比度,原图为1,大于1则越清晰,小于1则越模糊
  46. Text('对比度').fontSize(12).fontColor('#36D').width('90%')
  47. Image($r('app.media.my_border')).width('90%').height(40).contrast(2.0)
  48. Image($r('app.media.my_border')).width('90%').height(40).contrast(0.5)
  49. }
  50. .width('100%')
  51. }
  52. ListItem(){
  53. Column({space:5}){
  54. // 图像反转比例
  55. Text('图像反转').fontSize(12).fontColor('#36D').width('90%')
  56. Image($r('app.media.my_border')).width('90%').height(40).invert(0.2)
  57. Image($r('app.media.my_border')).width('90%').height(40).invert(0.8)
  58. }
  59. .width('100%')
  60. }
  61. ListItem(){
  62. Column({space:5}){
  63. // 叠加添色
  64. Text('叠加添色').fontSize(12).fontColor('#36D').width('90%')
  65. Image($r('app.media.my_border')).width('90%').height(40).colorBlend(Color.Green)
  66. Image($r('app.media.my_border')).width('90%').height(40).colorBlend(Color.Blue)
  67. }
  68. .width('100%')
  69. }
  70. ListItem(){
  71. Column({space:5}){
  72. // 深褐色
  73. Text('深褐色').fontSize(12).fontColor('#36D').width('90%')
  74. Image($r('app.media.my_border')).width('90%').height(40).sepia(0.8)
  75. }
  76. .width('100%')
  77. }
  78. ListItem(){
  79. Column({space:5}){
  80. // 色相旋转
  81. Text('色相旋转').fontSize(12).fontColor('#36D').width('90%')
  82. Image($r('app.media.my_border')).width('90%').height(40).hueRotate(90)
  83. }
  84. .width('100%')
  85. }
  86. }
  87. .width('100%')
  88. .margin({top: 5})
  89. }
  90. }

1.15、形状裁剪

用于对组件进行裁剪、遮罩处理

名称参数类型描述
clipCircle | Ellipse | Path | Rect | boolean

为相应类型的组件,按指定的形状对当前组件进行裁剪

参数为boolean类型时,设置是否按照父容器边缘轮廓进行裁

默认值:false

支持在卡片中使用

maskCircle | Ellipse | Path | Rect在当前组件上加上指定形状的遮罩

  1. @Entry
  2. @Component
  3. struct ClipTest {
  4. build() {
  5. Column({space: 15}) {
  6. Text('clip').fontSize(12).width('90%').fontColor('#36D')
  7. Row() {
  8. Image($r('app.media.my_border'))
  9. .width('500px')
  10. .height('300px')
  11. }
  12. .clip(true) // 这里要设置为true,否则下方的圆角不会生效
  13. .borderRadius(35)
  14. Image($r('app.media.my_border'))
  15. .width('500px')
  16. .height('300px')
  17. // 使用一个300px直径的圆对图片进行裁剪
  18. .clip(new Circle({width: '300px', height: '300px'}))
  19. Text('mask').fontSize(12).width('90%').fontColor('#36D')
  20. Image($r('app.media.my_border'))
  21. .width('500px')
  22. .height('300px')
  23. //使用一个500*300的方形进行遮罩
  24. .mask(new Rect({width:'500px', height: '300px'}).fill(Color.Gray))
  25. }
  26. .width('100%')
  27. .margin({top: 15})
  28. }
  29. }

 1.16、文本样式

用来设置文的样式

名称参数类型描述
fontColorResourceColor

设置字体的颜色

支持在卡片中使用

fontSizeLength

设置字体的大小

单位为fp

默认值:16

支持在卡片中使用

fontStyleFontStyle

设置字体的样式

默认值是:FontStyle.Normal

支持在卡片中使用

fontWeightnumber | FontWeight | string

设置字休的粗细

number值取值范围[100,900],取值间隔是100,默认值是400

FontWeight中是字体粗细的枚举值:"bold"、"bolder"、"lighter"、"regular"、"medium"

默认是:FontWeight.Normal

支持在卡片中使用

fontFamilystring | Resouce

设置字体列表

默认字体:'HarmonyOS Sans'

支持卡片中使用

lineHeightstring | number | Resource

设置文本的行高

设置值不大于0时,不限制文本行高,自适应字体大小,Length为number类型时单位为fp

支持卡片中使用

decoration

{

  type:TextDecorationType

  color?:ResourceColor

}

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

默认值:

{

type: TextDecorationType.None,

color:Color.Black

}

支持卡片中使用

 

  1. @Entry
  2. @Component
  3. struct TextStyleTest {
  4. build() {
  5. Column({space: 5}){
  6. Text('默认文本')
  7. Text('红色字体文本').fontColor(Color.Red)
  8. Text('默认字体大小')
  9. Text('字体大小为10pf').fontSize(10)
  10. Text('斜体样式文本').fontStyle(FontStyle.Italic)
  11. Text('字体粗细为700').fontWeight(700)
  12. Text('字体粗细为Bolder').fontWeight(FontWeight.Bolder)
  13. Text('字本装饰器').decoration({
  14. type:TextDecorationType.LineThrough,
  15. color:Color.Red
  16. })
  17. }
  18. .width('100%')
  19. .padding({top:5})
  20. }
  21. }

1.17、渐变颜色

设置组件的颜色渐变效果

名称参数说明描述
linearGradient

{

  angle?:number|string,

  direction?:GradientDirection,

  colors:Array<[ResourceColor,number]>,

  repeating?:boolean

}

线性渐变

angle:线性渐变的起始角度。0点方向顺时针旋转为正向角度

黑认值:180

direction:线性渐变的方向,设置过angle则不生效

默认值:GradientDirection.Bottom

colors:渐变色描述

repeating:为渐变的颜色重复着色

默认值:false

支持卡片使用

sweepGradient

{

  center:Point,

  start?:number | string,

  end?:number | string,

  rotation?:number | string,

  colors:Array<[ResourceColor,number]>,

  repeating?:boolean

}

角度渐变

center:角度渐变的中心点,相对于当前组件左上角的坐标

start:角度渐变的起点,默认值:0

end:角度渐变的终点,默认值:0

rotation:角度渐变的旋转角度,默认值:0

colors:渐变色的颜色描述

repeating:为渐变的颜色重复着色,默认值:false

支持在卡片中使用

注意:设置为小于0的值时,按值为0处理。设置为大于360的值时,按值为360处理。当start、end、rotation的数据类型为string,值为"90"或"90%",与90效果一致

radialGradient

{

  center:Point,

  radius:number | string,

  colors:Array<[ResourceColor,number]>,

repeating?:boolean

}

径向渐变

center:径向渐变的中心点,相对于当前组件左上角的坐标

radius:径向渐变的半径,取值范围[0,+∞}

设为小于0的值时,按0处理

color:渐变的颜色描述

repeating:渐变颜色重复着色

默认值:false

支持卡片中使用

  1. @Entry
  2. @Component
  3. struct ColorGradientTest {
  4. build() {
  5. Column({ space: 10 }) {
  6. Text('linearGradient').fontSize(12).fontColor('#36D').width('90%')
  7. Row()
  8. .width('90%')
  9. .height(30)
  10. .linearGradient({
  11. angle:90,
  12. colors:[['#ef5b9c',0.0],['#f47920',0.6],['#426ab3',1.0]]
  13. })
  14. Text('linearGradient Repeat').fontSize(12).width('90%').fontColor('#36D')
  15. Row()
  16. .width('90%')
  17. .height(30)
  18. .linearGradient({
  19. direction: GradientDirection.Left, // 渐变的方向
  20. // 当颜色数组元素占比小于1时可以使用重复着色,如果不重复则最后一段会使用最后一个颜色值
  21. colors:[['#ef5b9c',0.0],['#f47920',0.3],['#426ab3',0.5]],
  22. repeating:true
  23. })
  24. Text('sweepGradient').fontSize(12).fontColor('#36D').width('90%')
  25. Row()
  26. .width(100)
  27. .height(100)
  28. .sweepGradient({
  29. center:[50,50],
  30. start:0,
  31. end:359,
  32. colors:[['#ef5b9c',0.0],['#f47920',0.6],['#426ab3',1.0]]
  33. })
  34. Text('radialGradient Repeat').fontSize(12).fontColor('#36D').width('90%')
  35. Row()
  36. .width(100)
  37. .height(100)
  38. .radialGradient({
  39. center:[50,50],
  40. radius:60,
  41. colors:[['#ef5b9c',0.0],['#f47920',0.3],['#426ab3',0.5]],
  42. repeating:true
  43. })
  44. }.width('100%').padding({ top: 5 })
  45. }
  46. }

 1.18、Popup控制

为组件绑定popup弹窗,并设置弹窗的内容,交互逻辑和显示状态

名称参数类型描述
bindPopup

show:boolean

popup:PopupOptions | CustomPopupOptions

给组件绑定Popup弹窗

show为true弹出弹窗,默认值是false

popup:配置当前弹窗的提示参数

注意:popup弹窗必须等待页面全部构建完成才能展示,因此show不能在页面构建中设置为true,否则会导致popup弹窗显示位置及形状错误

 PopupOptions类型说明

名称类型描述
messagestring

必填

弹窗的信息内空

placementOnTopboolean

非必填

是否在组件上方显示,默认是false

primaryButton

{

  value:string,

  action:() => void

}

非必填

第一个按钮

value:按钮文本

action:点击时的回调函数

secondaryButton

{

  value:string,

  action:() => void

}

非必填

第二个按钮

value:按钮文本

action:点击时的回调函数

onStateChange(event:{isVisible:boolean}) => void

非必填

弹窗状态变化事件回调,参数isVisible为弹窗当前的显示状态

arrowOffsetlength

非必填

popup箭头在弹窗处的偏移。箭头在气泡上下方时,数值为0表示箭头居最左侧,偏移量为箭头至最左侧的距离,默认居中。箭头在气泡左右侧时,偏移量为箭头至最上侧的距离,默认居中。如果显示在屏幕边缘,气泡会自动左右偏移,数值为0时箭头始终指向绑定组件

showInSubWindowboolean

非必填

是否在子窗口显示气泡

默认值为false。

CustomPopupOptions类型说明

名称类型说明描述
builderCustomBuilder

必填

提示气泡内容的构造器

placementPlacement

非必填

气泡组件优先显示的位置,当前位置显示不下时,会自动调整位置

默认值:Placement.Bottom

popupColorResourceColor

非必填

提示气泡的颜色

enableArrowboolean

非必填

是否显示箭头

默认值:true

autoCancelboolean

非必填

页面有操作时,是否自动关闭气泡

默认值:true

onStateChange(event:{isVisible:boolean})=>void

非必填

弹窗状态变化事件回调,参数为弹窗当前的显示状态

arrowOffsetLength

非必填

popup箭头在弹窗处的偏移

showInSubWindowboolean

非必填

是否在子窗口显示气泡,默认值为false

 

  1. import Prompt from '@system.prompt';
  2. @Entry
  3. @Component
  4. struct PoupTest {
  5. @State handlePopup: boolean = false;
  6. @State customPopup: boolean = false;
  7. // popup构造器定义弹框内容
  8. @Builder popupBuilder() {
  9. Row({space: 2}) {
  10. Image($r('app.media.my_border')).width(24).height(24).margin({left: -5})
  11. Text('Custom Popup').fontSize(12)
  12. }.width(120).height(50).padding(5)
  13. }
  14. build() {
  15. Flex({direction: FlexDirection.Column}) {
  16. // PopupOptions类型设置弹框的内容
  17. Button('PopupOptions')
  18. .onClick(() => {
  19. this.handlePopup = !this.handlePopup;
  20. })
  21. .bindPopup(this.handlePopup,{
  22. message: '使用PopupOptions来设置弹框内容',
  23. placementOnTop: true, // 是否在组件上方显示
  24. showInSubWindow: false, // 是否在子窗口显示气泡
  25. primaryButton: {
  26. // 第一个按钮
  27. value: '确定',
  28. action: () => {
  29. this.handlePopup = !this.handlePopup
  30. Prompt.showToast({
  31. message: '点击了弹窗中的【确定】按钮',
  32. duration: 2000
  33. })
  34. }
  35. },
  36. secondaryButton: {
  37. // 第二个按钮
  38. value: '取消',
  39. action: () => {
  40. this.handlePopup = !this.handlePopup
  41. Prompt.showToast({
  42. message: '点击了弹窗中的【取消】按钮',
  43. duration: 2000
  44. })
  45. }
  46. },
  47. onStateChange: (e) => {
  48. if(!e.isVisible) {
  49. this.handlePopup = false
  50. }
  51. }
  52. })
  53. .position({x:100, y:50})
  54. // CustomPopupOptions类型设置弹框内容
  55. Button('CustomPopupOptions')
  56. .onClick(() => {
  57. this.customPopup = !this.customPopup;
  58. })
  59. .bindPopup(this.customPopup,{
  60. builder: this.popupBuilder(), // 提示气泡内容的构造器
  61. placement: Placement.Top, // 气泡组件优先显示的位置
  62. maskColor: '#6a6da9',
  63. popupColor: Color.Yellow, // 提示气泡的颜色
  64. enableArrow: true, // 是否显示箭头
  65. showInSubWindow: false,
  66. onStateChange: (e) => {
  67. if(!e.isVisible) {
  68. this.customPopup = false
  69. }
  70. }
  71. })
  72. .position({x: 80, y: 200})
  73. }
  74. .width('100%').padding({top:5})
  75. }
  76. }

 1.19、菜单

为组件绑定弹出式菜单,弹出式菜单以垂直列表形式显示菜单项,可通过长按、点击或鼠标右键触发

名称参数类型描述
bindMenuArray<MenuIte> | CustomBuilder

给组件绑定菜单,点击后弹出菜单

弹出的菜单支持文本和自定义菜单两种

bindContextMenu

content:CustomBilder

responseType:ResponseType

给组件绑定菜单,触发方式为长按或者右键点击,弹出菜单项需要自定义。

MenuItem说明

名称类型描述
valuestring菜单项文本
action() => void点击菜单项的事件回调

  1. import Prompt from '@system.prompt'
  2. @Entry
  3. @Component
  4. struct MenuTest {
  5. @State listData: number[] = [0,0,0]
  6. @Builder MenuBuilder() {
  7. Flex({direction:FlexDirection.Column,justifyContent:FlexAlign.Center,alignItems:ItemAlign.Center}) {
  8. ForEach(this.listData,(item,index) => {
  9. Column() {
  10. Row() {
  11. Image($r('app.media.icon')).width(20).height(20).margin({right:5})
  12. Text(`Menu${index + 1}`).fontSize(20)
  13. }
  14. .height(30)
  15. .justifyContent(FlexAlign.Center)
  16. .align(Alignment.Center)
  17. .onClick(() => {
  18. Prompt.showToast({
  19. message: `你点击了【Menu${index + 1}】`
  20. })
  21. })
  22. if(index != this.listData.length - 1) {
  23. Divider().height(10).width('80%').color('#ccc')
  24. }
  25. }.padding(5).height(40)
  26. })
  27. }
  28. }
  29. build() {
  30. Column(){
  31. Row(){
  32. Text('click for Menu').fontSize(12).fontColor('#36D')
  33. .bindMenu([
  34. {
  35. value: '菜单一',
  36. action:() => {
  37. Prompt.showToast({
  38. message: '点击了菜单一',
  39. duration: 2000
  40. })
  41. }
  42. },
  43. {
  44. value: '菜单二',
  45. action:() => {
  46. Prompt.showToast({
  47. message: '点击了菜单二',
  48. duration: 2000
  49. })
  50. }
  51. }
  52. ])
  53. Text('click for Menu').fontSize(12).fontColor('#36D')
  54. .bindMenu(this.MenuBuilder())
  55. }
  56. .width('80%')
  57. .justifyContent(FlexAlign.SpaceAround)
  58. }.height('100%').padding(5)
  59. }
  60. }

1.20、焦点控制

自定义组件的走焦效果,可设置组件是否走焦和具体的走焦顺序,tab键或者方向键切换焦点

名称参数类型描述
focusableboolean设置当前组件是否可以获焦
tabIndexnumber

自定义组件tab键走焦能力

tabIndex >= 0:表示元素是可聚焦的,并且可以通过tab键走焦来访问到该元素

tabIndex < 0(通常是tabIndex = -1):表示元素是可聚焦的,但是不能通过tab键走焦来访问到该元素

默认值:0

defaultFocusboolean

设置当前组件是否为当前页面上的默认焦点

默认值:false

groupDefaultFocusboolean

设置当前组件是否为当前组件所在容器获焦时的默认焦点

默认值:false

focusOnTouchboolean

设置当前组件是否支持点击获焦能力

默认值:false

focusControl

requestFocus(value:string):boolean

方法语句中可使用的全局接口,调用此接口可以主动让焦点转移至参数指定的组件上

返回值boolean表示是否成功给目标组件申请到焦点

焦点事件当前仅支持在真机上显示运行效果。

1.21、悬浮态

设置组件的鼠标悬浮态显示效果

属性:hoverEffect

参数类型:HoverEffect

设置当前组件悬停态下的悬浮效果。

默认值:HoverEffect.Auto

  1. @Entry
  2. @Component
  3. struct HoverTest {
  4. @State isHoverVal: boolean = false
  5. build() {
  6. Column({ space: 5 }) {
  7. Column({ space: 5 }) {
  8. Text('Scale').fontSize(20).fontColor(Color.Gray).width('90%').position({ x: 0, y: 80 })
  9. Column()
  10. .width('80%').height(200).backgroundColor(Color.Gray)
  11. .position({ x: 40, y: 120 })
  12. .hoverEffect(HoverEffect.Scale)
  13. .onHover((isHover: boolean) => {
  14. console.info('Scale isHover: ' + isHover)
  15. this.isHoverVal = isHover
  16. })
  17. Text('Board').fontSize(20).fontColor(Color.Gray).width('90%').position({ x: 0, y: 380 })
  18. Column()
  19. .width('80%').height(200).backgroundColor(Color.Gray)
  20. .hoverEffect(HoverEffect.Highlight)
  21. .position({ x: 40, y: 420 })
  22. .onHover((isHover: boolean) => {
  23. console.info('Highlight isHover: ' +isHover )
  24. this.isHoverVal = isHover
  25. })
  26. }
  27. .hoverEffect(HoverEffect.None)
  28. .width('100%').height('100%').border({ width: 1 })
  29. .onHover((isHover: boolean) => {
  30. console.info('HoverEffect.None')
  31. this.isHoverVal = isHover
  32. })
  33. }
  34. }
  35. }

1.22、组件标识

id为组件的唯一标识,在整个应用内唯一。本模块提供组件标识相关接口,可以获取指定id组件的属性,也提供向指定id组件发送事件的功能。

getInspectorByKey

getInspectorByKey(id: string): string

获取指定id的组件的所有属性,不包含子组件信息。

返回的是一个组件属性列表的JSON字符串

getInspectorTree

getInspectorTree(): Object

获取组件树及组件属性

返回的是组件树及组件属性列表的JSON对象

sendEventByKey

sendEventByKey(id: string, action: number, params: string): boolean

给指定id的组件发送事件

action支持取值:10表示点击事件;11表示长按事件

params:为事件参数,无参时传空字符串

返回值,找不到指定id的组件时返回false,其余情况返回true

sendTouchEvent

sendTouchEvent(event: TouchObject): boolean

发送触摸事件

返回值,事件发送失败时返回false,其余情况返回true

sendKeyEvent

sendKeyEvent(event: KeyEvent): boolean

发送按键事件

返回值,事件发送失败时返回false,其余情况返回true

sendMouseEvent

sendMouseEvent(event: MouseEvent): boolean

发送鼠标事件

返回值,事件发送失败时返回false,其余情况返回true

1.23、触摸热区

适用于支持通用点击事件、通用触摸事件、通用手势处理的组件

名称参数类型描述
responseRegionArray<Rectangle> | Retangle

设置一个或多个触摸热区,包括位置和大小

默认触摸热区为整个组件,默认值

{

x:0,       // 触摸点相对于组件左上角的x轴坐标

y:0,        // 触摸点相对于组件左上角的y轴坐标

width:'100%',        // 触摸热区的宽度

height:'100%'        // 触摸热区的高度

}

支持在卡片中使用

1.24、多态样式

设置组件不同状态下的样式

在组件结构体中,使用@Styles 的样式方法,如下所示:

  1. @Styles pressedStyles() {
  2. .backgroundColor("#ED6F21")
  3. .borderRadius(10)
  4. .borderStyle(BorderStyle.Dashed)
  5. .borderWidth(2)
  6. .borderColor("#33000000")
  7. .width(120)
  8. .height(30)
  9. .opacity(1)
  10. }

在组件中,使用stateStyles来引入定义的样式,如下所示:

  1. Text("pressed")
  2. .backgroundColor("#0A59F7")
  3. .borderRadius(20)
  4. .borderStyle(BorderStyle.Dotted)
  5. .borderWidth(2)
  6. .borderColor(Color.Red)
  7. .width(100)
  8. .height(25)
  9. .opacity(1)
  10. .fontSize(14)
  11. .fontColor(Color.White)
  12. .stateStyles({
  13. pressed: this.pressedStyles,
  14. })

1.25、触摸测试控制

设置组件的触摸测试类型。ArkUI开发框架在处理触屏事件时,会在触屏事件触发前,进行按压点和组件区域的触摸测试来收集需要响应触屏事件的组件,然后基于触摸测试结果分发相应的触屏事件。hitTestBehavior属性可以设置不同的触摸测试响应模式,影响组件的触摸测试收集结果,最终影响后续的触屏事件分发。

HitTestMode枚举值有如下:

Default:默认触摸测试效果,自身和子节点都响应触摸测试,但会阻塞兄弟节点的触摸测试

Block:自身响应触摸测试,阻塞子节点和兄弟节点的触摸测试

Transparent:自身和子节点都响应触摸测试,不会阻塞兄弟节点的触摸测试

None:自身不响应触摸测试,不会阻塞子节点和兄弟节点的触摸测试

1.26、分布式迁移标识

组件的分布式迁移标识,指明了该组件在分布式迁移场景下可以将特定状态恢复到对端设备

restoreId: number类型,标记支持分布式迁移的组件Id,用于两端设备组件的配对。同一个应用中各个支持分布式迁移组件的Id必须不同

支持组件:List,Grid,Scorll

1.27、点击控制

设置组件是否可以响应点击事件、触摸事件等手指交互事件

touchable:boolean类型,设置当前组件是否可以响应点击事件、触摸事件等手指交互事件,默认是true

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/199733
推荐阅读
相关标签
  

闽ICP备14008679号