鸿蒙HarmonyOS(ArkUI基础-3)


(今天更新的内容是通用布局属性与弹性布局)

ArkUI(方舟UI框架)

1.简介

ArkUI(方舟UI框架)为应用的UI开发提供了完整的基础设施,包括简洁的UI语法、丰富的UI功能(组件、布局、动画以及交互事件),以及实时界面预览工具等,可以支持开发者进行可视化界面开发。

2.基本概念

  • UI: 即用户界面。开发者可以将应用的用户界面设计为多个功能页面,每个页面进行单独的文件管理,并通过页面路由API完成页面间的调度管理如跳转、回退等操作,以实现应用内的功能解耦。
  • 组件: UI构建与显示的最小单位,如列表、网格、按钮、单选框、进度条、文本等。开发者通过多种组件的组合,构建出满足自身应用诉求的完整界面。

3.概述

  • ArkTS

    ArkTS是优选的主力应用开发语言,围绕应用开发在TypeScript(简称TS)生态基础上做了进一步扩展。扩展能力包含声明式UI描述、自定义组件、动态扩展UI元素、状态管理和渲染控制。状态管理作为基于ArkTS的声明式开发范式的特色,通过功能不同的装饰器给开发者提供了清晰的页面更新渲染流程和管道。状态管理包括UI组件状态和应用程序状态,两者协作可以使开发者完整地构建整个应用的数据更新和UI渲染。ArkTS语言的基础知识请参考初识ArkTS语言

  • 布局

    布局是UI的必要元素,它定义了组件在界面中的位置。ArkUI框架提供了多种布局方式,除了基础的线性布局、层叠布局、弹性布局、相对布局、栅格布局外,也提供了相对复杂的列表、宫格、轮播。

  • 组件

    组件是UI的必要元素,形成了在界面中的样子,由框架直接提供的称为系统组件,由开发者定义的称为自定义组件。系统内置组件包括按钮、单选框、进度条、文本等。开发者可以通过链式调用的方式设置系统内置组件的渲染效果。开发者可以将系统内置组件组合为自定义组件,通过这种方式将页面组件化为一个个独立的UI单元,实现页面不同单元的独立创建、开发和复用,具有更强的工程性。

  • 页面路由和组件导航

    应用可能包含多个页面,可通过页面路由实现页面间的跳转。一个页面内可能存在组件间的导航如典型的分栏,可通过导航组件实现组件间的导航。

  • 图形

    方舟开发框架提供了多种类型图片的显示能力和多种自定义绘制的能力,以满足开发者的自定义绘图需求,支持绘制形状、填充颜色、绘制文本、变形与裁剪、嵌入图片等。

  • 动画

    动画是UI的重要元素之一。优秀的动画设计能够极大地提升用户体验,框架提供了丰富的动画能力,除了组件内置动画效果外,还包括属性动画、显式动画、自定义转场动画以及动画API等,开发者可以通过封装的物理模型或者调用动画能力API来实现自定义动画轨迹。

  • 交互事件

    交互事件是UI和用户交互的必要元素。方舟开发框架提供了多种交互事件,除了触摸事件、鼠标事件、键盘按键事件、焦点事件等通用事件外,还包括基于通用事件进行进一步识别的手势事件。手势事件有单一手势如点击手势、长按手势、拖动手势、捏合手势、旋转手势、滑动手势,以及通过单一手势事件进行组合的组合手势事件。

  • 自定义能力

    自定义能力是UI开发框架提供给开发者对UI界面进行开发和定制化的能力。包括:自定义组合、自定义扩展、自定义节点和自定义渲染。

4.布局

1.概述

(与前端的基本布局差不多,有前端基础的小伙伴很好上手的)

布局通常为分层结构,一个常见的页面结构如下所示:

为实现上述效果,开发者需要在页面中声明对应的元素。其中,Page表示页面的根节点,Column/Row等元素为系统组件。针对不同的页面结构,ArkUI提供了不同的布局组件来帮助开发者实现对应布局的效果,例如Row用于实现线性布局。

2.布局元素的组成

布局相关的容器组件可形成对应的布局效果。例如,List组件可构成线性布局。

布局元素组成图

  • 组件区域(蓝区方块):组件区域表示组件的大小,widthheight属性用于设置组件区域的大小。
  • 组件内容区(黄色方块):组件内容区大小为组件区域大小减去组件的border值,组件内容区大小会作为组件内容(或者子组件)进行大小测算时的布局测算限制。
  • 组件内容(绿色方块):组件内容本身占用的大小,比如文本内容占用的大小。组件内容和组件内容区不一定匹配,比如设置了固定的width和height,此时组件内容的大小就是设置的width和height减去padding和border值,但文本内容则是通过文本布局引擎测算后得到的大小,可能出现文本真实大小小于设置的组件内容区大小。当组件内容和组件内容区大小不一致时,align属性生效,定义组件内容在组件内容区的对齐方式,如居中对齐。
  • 组件布局边界(虚线部分):组件通过margin属性设置外边距时,组件布局边界就是组件区域加上margin的大小。

3.所有布局样式

布局应用场景
线性布局(Row、Column)如果布局内子元素超过1个时,且能够以某种方式线性排列时优先考虑此布局。
层叠布局(Stack)组件需要有堆叠效果时优先考虑此布局。层叠布局的堆叠效果不会占用或影响其他同容器内子组件的布局空间。例如Panel作为子组件弹出时将其他组件覆盖更为合理,则优先考虑在外层使用堆叠布局。
弹性布局(Flex)弹性布局是与线性布局类似的布局方式。区别在于弹性布局默认能够使子组件压缩或拉伸。在子组件需要计算拉伸或压缩比例时优先使用此布局,可使得多个容器内子组件能有更好的视觉上的填充效果。
相对布局(RelativeContainer)相对布局是在二维空间中的布局方式,不需要遵循线性布局的规则,布局方式更为自由。通过在子组件上设置锚点规则(AlignRules)使子组件能够将自己在横轴、纵轴中的位置与容器或容器内其他子组件的位置对齐。设置的锚点规则可以天然支持子元素压缩、拉伸、堆叠或形成多行效果。在页面元素分布复杂或通过线性布局会使容器嵌套层数过深时推荐使用。
栅格布局(GridRow、GridCol)栅格是多设备场景下通用的辅助定位工具,可将空间分割为有规律的栅格。栅格不同于网格布局固定的空间划分,可以实现不同设备下不同的布局,空间划分更随心所欲,从而显著降低适配不同屏幕尺寸的设计及开发成本,使得整体设计和开发流程更有秩序和节奏感,同时也保证多设备上应用显示的协调性和一致性,提升用户体验。推荐内容相同但布局不同时使用。
媒体查询(@ohos.mediaquery)媒体查询可根据不同设备类型或同设备不同状态修改应用的样式。例如根据设备和应用的不同属性信息设计不同的布局,以及屏幕发生动态改变时更新应用的页面布局。
列表(List)使用列表可以高效地显示结构化、可滚动的信息。在ArkUI中,列表具有垂直和水平布局能力和自适应交叉轴方向上排列个数的布局能力,超出屏幕时可以滚动。列表适合用于呈现同类数据类型或数据类型集,例如图片和文本。
网格(Grid)网格布局具有较强的页面均分能力、子元素占比控制能力。网格布局可以控制元素所占的网格数量、设置子元素横跨几行或者几列,当网格容器尺寸发生变化时,所有子元素以及间距等比例调整。推荐在需要按照固定比例或者均匀分配空间的布局场景下使用,例如计算器、相册、日历等。
轮播(Swiper)轮播组件通常用于实现广告轮播、图片预览等。
选项卡(Tabs)选项卡可以在一个页面内快速实现视图内容的切换,一方面提升查找信息的效率,另一方面精简用户单次获取到的信息量。

3.对子元素的约束

约束使用场景实现方式
拉伸容器组件尺寸发生变化时,增加或减小的空间全部分配给容器组件内指定区域。flexGrowflexShrink属性:1. flexGrow基于父容器的剩余空间分配来控制组件拉伸。2. flexShrink设置父容器的压缩尺寸来控制组件压缩。
缩放子组件的宽高按照预设的比例,随容器组件发生变化,且变化过程中子组件的宽高比不变。aspectRatio属性指定当前组件的宽高比来控制缩放,公式为:aspectRatio=width/height。
占比占比能力是指子组件的宽高按照预设的比例,随父容器组件发生变化。基于通用属性的两种实现方式:1. 将子组件的宽高设置为父组件宽高的百分比。2. layoutWeight属性,使得子元素自适应占满剩余空间。
隐藏隐藏能力是指容器组件内的子组件,按照其预设的显示优先级,随容器组件尺寸变化显示或隐藏,其中相同显示优先级的子组件同时显示或隐藏。通过displayPriority属性来控制组件的显示和隐藏。

🎈🎈2.通用布局属性

1.常用的属性

属性描述
padding内边距
margin外边距
border边框线
borderRadius边框圆角

2.内边距 padding

​ 属性:数字 或 对象{}

  • 数字:上下左右内边距相同

  • 对象{}:配合 left、right、top、bottom 单独设置某个方向内边距

    @Entry
    @Component
    struct Index {
      build() {
        Column() {
          Text('文字')
            .backgroundColor(Color.Pink)
            // 单值:四个方向padding相同
            .padding(20)
            // 对象:单独设置某个方向
            .padding({
              top: 10,
              right: 20,
              bottom: 40,
              left: 80
            })
        }
      }
    }
    
    1. 外边距 margin

    2. 边框属性

      属性:border()

      参数:{width?: 数字, color?: '', style?: BorderStyle}

      • width:边框宽度,边框宽度默认值为0,即不显示边框

      • color:边框颜色

      • style:边框样式,BorderStyle枚举类型

        • Solid:实线(默认)
        • Dashed:虚线
        • Dotted:点线

5.边框圆角

属性:borderRadius(圆角半径)

参数:数值 或 { }

  • topLeft:左上角
  • topRight:右上角
  • bottomLeft:左下角
  • bottomRight:右下角
// xxx.ets
@Entry
@Component
struct BorderExample {
  build() {
    Column() {
      Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
        // 线段
        Text('dashed')
          .borderStyle(BorderStyle.Dashed).borderWidth(5).borderColor(0xAFEEEE).borderRadius(10)
          .width(120).height(120).textAlign(TextAlign.Center).fontSize(16)
        // 点线
        Text('dotted')
          .border({ width: 5, color: 0x317AF7, radius: 10, style: BorderStyle.Dotted })
          .width(120).height(120).textAlign(TextAlign.Center).fontSize(16)
      }.width('100%').height(150)

      Text('.border')
        .fontSize(50)
        .width(300)
        .height(300)
        .border({
          width: { left: 3, right: 6, top: 10, bottom: 15 },
          color: { left: '#e3bbbb', right: Color.Blue, top: Color.Red, bottom: Color.Green },
          radius: { topLeft: 10, topRight: 20, bottomLeft: 40, bottomRight: 80 },
          style: {
            left: BorderStyle.Dotted,
            right: BorderStyle.Dotted,
            top: BorderStyle.Solid,
            bottom: BorderStyle.Dashed
          }
        }).textAlign(TextAlign.Center)
    }
  }
}

6.背景图片

属性描述
backgroundColor背景色
backgroundImage背景图
backgroundImageSize背景图尺寸
backgroundImagePosition背景图位置
// xxx.ets
@Entry
@Component
struct BackgroundExample {

  build() {
    Column({ space: 5 }) {
      Text('background color').fontSize(9).width('90%').fontColor(0xCCCCCC)
      Row().width('90%').height(50).backgroundColor(0xE5E5E5).border({ width: 1 })

      Text('background image repeat along X').fontSize(9).width('90%').fontColor(0xCCCCCC)
      Row()
        .backgroundImage('/comment/bg.jpg', ImageRepeat.X)
        .backgroundImageSize({ width: '250px', height: '140px' })
        .width('90%')
        .height(70)
        .border({ width: 1 })

      Text('background image repeat along Y').fontSize(9).width('90%').fontColor(0xCCCCCC)
      Row()
        .backgroundImage('/comment/bg.jpg', ImageRepeat.Y)
        .backgroundImageSize({ width: '500px', height: '120px' })
        .width('90%')
        .height(100)
        .border({ width: 1 })

      Text('background image size').fontSize(9).width('90%').fontColor(0xCCCCCC)
      Row()
        .width('90%').height(150)
        .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
        .backgroundImageSize({ width: 1000, height: 500 })
        .border({ width: 1 })

      Text('background fill the box(Cover)').fontSize(9).width('90%').fontColor(0xCCCCCC)
      // 不保证图片完整的情况下占满盒子
      Row()
        .width(200)
        .height(50)
        .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
        .backgroundImageSize(ImageSize.Cover)
        .border({ width: 1 })

      Text('background fill the box(Contain)').fontSize(9).width('90%').fontColor(0xCCCCCC)
      // 保证图片完整的情况下放到最大
      Row()
        .width(200)
        .height(50)
        .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
        .backgroundImageSize(ImageSize.Contain)
        .border({ width: 1 })

      Text('background image position').fontSize(9).width('90%').fontColor(0xCCCCCC)
      Row()
        .width(100)
        .height(50)
        .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
        .backgroundImageSize({ width: 1000, height: 560 })
        .backgroundImagePosition({ x: -500, y: -300 })
        .border({ width: 1 })
    }
    .width('100%').height('100%').padding({ top: 5 })
  }
}

7.颜色渐变

​ 1.线性渐变 linearGradient()

{
	angle?:  线性渐变的起始角度,
	direction?: 线性渐变的方向,
	colors: [[颜色1, 颜色1所处位置], [颜色2, 颜色2所处位置], ......],
	repeating?: 是否重复着色
}
  • angle:线性渐变的起始角度。0点方向顺时针旋转为正向角度,默认值:180

  • direction: 线性渐变的方向,设置 angle 后不生效,值为 枚举类型 GradientDirection

    • Left:从右向左

    • Top:从下向上

    • Right:从左向右

    • Bottom:从上向下

    • LeftTop:从右下 到 左上

    • LeftBottom:从右上 到 左下

    • RightTop:从左下 到 右上

    • RightBottom:从左上 到 右下

      // xxx.ets
      @Entry
      @Component
      struct ColorGradientExample {
        build() {
          Column({ space: 5 }) {
            Text('linearGradient').fontSize(12).width('90%').fontColor(0xCCCCCC)
            Row()
              .width('90%')
              .height(50)
              .linearGradient({
                angle: 90,
                colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
              })
            Text('linearGradient Repeat').fontSize(12).width('90%').fontColor(0xCCCCCC)
            Row()
              .width('90%')
              .height(50)
              .linearGradient({
                direction: GradientDirection.Left, // 渐变方向
                repeating: true, // 渐变颜色是否重复
                colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 0.5]] // 数组末尾元素占比小于1时满足重复着色效果
              })
          }
          .width('100%')
          .padding({ top: 5 })
        }
      }
      

      2.径向渐变 radialGradient()

      {
      	center:  径向渐变的中心点坐标,
      	radius: 径向渐变的半径,
      	colors: [[颜色1, 颜色1所处位置], [颜色2, 颜色2所处位置], ......],
      	repeating?: 是否重复着色
      }
      
      • center:径向渐变的中心点,即相对于当前组件左上角的坐标,写法[x坐标, y坐标]

        // xxx.ets
        @Entry
        @Component
        struct ColorGradientExample {
          build() {
            Column({ space: 5 }) {
              Text('radialGradient').fontSize(12).width('90%').fontColor(0xCCCCCC)
              Row()
                .width(100)
                .height(100)
                .radialGradient({
                  center: [50, 50],
                  radius: 60,
                  colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
                })
                
              Text('radialGradient Repeat').fontSize(12).width('90%').fontColor(0xCCCCCC)
              Row()
                .width(100)
                .height(100)
                .radialGradient({
                  center: [50, 50],
                  radius: 60,
                  repeating: true,
                  colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 0.5]] // 数组末尾元素占比小于1时满足重复着色效果
                })
            }
            .width('100%')
            .padding({ top: 5 })
          }
        }
        

3.线性布局

1.概述

线性布局(LinearLayout)是开发中最常用的布局,通过线性容器RowColumn构建。线性布局是其他布局的基础,其子元素在线性方向上(水平方向和垂直方向)依次排列。线性布局的排列方向由所选容器组件决定,Column容器内子元素按照垂直方向排列,Row容器内子元素按照水平方向排列。根据不同的排列方向,开发者可选择使用Row或Column容器创建线性布局。

Column容器内子元素排列示意图

Row容器内子元素排列示意图

2.间距:space

//自己试一下吧
Column({ space: 20 }) {
  Text('space: 20').fontSize(15).fontColor(Color.Gray).width('90%')
  Row().width('90%').height(50).backgroundColor(0xF5DEB3)
  Row().width('90%').height(50).backgroundColor(0xD2B48C)
  Row().width('90%').height(50).backgroundColor(0xF5DEB3)
}.width('100%')

//自己试一下吧
Row({ space: 35 }) {
  Text('space: 35').fontSize(15).fontColor(Color.Gray)
  Row().width('10%').height(150).backgroundColor(0xF5DEB3)
  Row().width('10%').height(150).backgroundColor(0xD2B48C)
  Row().width('10%').height(150).backgroundColor(0xF5DEB3)
}.width('90%')

3.辅轴排列方式

在布局容器内,可以通过alignItems属性设置子元素在交叉轴(排列方向的垂直方向)上的对齐方式。且在各类尺寸屏幕中,表现一致。其中,交叉轴为垂直方向时,取值为VerticalAlign类型,水平方向取值为HorizontalAlign类型。

Column容器内子元素在水平方向上的排列图

//HorizontalAlign.Start:子元素在水平方向左对齐。

Column({}) {
  Column() {
  }.width('80%').height(50).backgroundColor(0xF5DEB3)

  Column() {
  }.width('80%').height(50).backgroundColor(0xD2B48C)

  Column() {
  }.width('80%').height(50).backgroundColor(0xF5DEB3)
}.width('100%').alignItems(HorizontalAlign.Start).backgroundColor('rgb(242,242,242)')

Row容器内子元素在垂直方向上的排列图

//VerticalAlign.Top:子元素在垂直方向顶部对齐。
Row({}) {
  Column() {
  }.width('20%').height(30).backgroundColor(0xF5DEB3)

  Column() {
  }.width('20%').height(30).backgroundColor(0xD2B48C)

  Column() {
  }.width('20%').height(30).backgroundColor(0xF5DEB3)
}.width('100%').height(200).alignItems(VerticalAlign.Top).backgroundColor('rgb(242,242,242)')

4.主轴排列方式

Column容器内子元素在垂直方向上的排列图

//去试一下吧
Column({}) {
  Column() {
  }.width('80%').height(50).backgroundColor(0xF5DEB3)

  Column() {
  }.width('80%').height(50).backgroundColor(0xD2B48C)

  Column() {
  }.width('80%').height(50).backgroundColor(0xF5DEB3)
}.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start)

Row容器内子元素在水平方向上的排列图

//去试一下吧
Row({}) {
  Column() {
  }.width('20%').height(30).backgroundColor(0xF5DEB3)

  Column() {
  }.width('20%').height(30).backgroundColor(0xD2B48C)

  Column() {
  }.width('20%').height(30).backgroundColor(0xF5DEB3)
}.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start)

5.自适应拉伸

在线性布局下,常用空白填充组件Blank,在容器主轴方向自动填充空白空间,达到自适应拉伸效果。Row和Column作为容器,只需要添加宽高为百分比,当屏幕宽高发生变化时,会产生自适应效果。

@Entry
@Component
struct BlankExample {
  build() {
    Column() {
      Row() {
        Text('Bluetooth').fontSize(18)
        Blank()
        Toggle({ type: ToggleType.Switch, isOn: true })
      }.backgroundColor(0xFFFFFF).borderRadius(15).padding({ left: 12 }).width('100%')
    }.backgroundColor(0xEFEFEF).padding(20).width('100%')
  }
}

6.自适应缩放

自适应缩放是指子元素随容器尺寸的变化而按照预设的比例自动调整尺寸,适应各种不同大小的设备。在线性布局中,可以使用以下两种方法实现自适应缩放。

  • 父容器尺寸确定时,使用layoutWeight属性设置子元素和兄弟元素在主轴上的权重,忽略元素本身尺寸设置,使它们在任意尺寸的设备下自适应占满剩余空间。
@Entry
@Component
struct layoutWeightExample {
  build() {
    Column() {
      Text('1:2:3').width('100%')
      Row() {
        Column() {
          Text('layoutWeight(1)')
            .textAlign(TextAlign.Center)
        }.layoutWeight(1).backgroundColor(0xF5DEB3).height('100%')

        Column() {
          Text('layoutWeight(2)')
            .textAlign(TextAlign.Center)
        }.layoutWeight(2).backgroundColor(0xD2B48C).height('100%')

        Column() {
          Text('layoutWeight(3)')
            .textAlign(TextAlign.Center)
        }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%')

      }.backgroundColor(0xffd306).height('30%')
      
    }
  }
}

7.自适应延伸

使用Scroll组件:在线性布局中,开发者可以进行垂直方向或者水平方向的布局。当一屏无法完全显示时,可以在Column或Row组件的外层包裹一个可滚动的容器组件Scroll来实现可滑动的线性布局。

//垂直方向布局中使用Scroll组件
@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller();
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

  build() {
    Scroll(this.scroller) {
      Column() {
        ForEach(this.arr, (item?:number|undefined) => {
          if(item){
            Text(item.toString())
            .width('90%')
            .height(150)
            .backgroundColor(0xFFFFFF)
            .borderRadius(15)
            .fontSize(16)
            .textAlign(TextAlign.Center)
            .margin({ top: 10 })
          }
        }, (item:number) => item.toString())
      }.width('100%')
    }
    .backgroundColor(0xDCDCDC)
    .scrollable(ScrollDirection.Vertical) // 滚动方向为垂直方向
    .scrollBar(BarState.On) // 滚动条常驻显示
    .scrollBarColor(Color.Gray) // 滚动条颜色
    .scrollBarWidth(10) // 滚动条宽度
    .edgeEffect(EdgeEffect.Spring) // 滚动到边沿后回弹
  }
}

-------------------------(第一次)今天的更新结束了,感觉对你有帮助的话就点点关注吧❤-----------------

🎈🎈4.弹性布局(Flex)

(使用线性布局一般就可以解决大部分的问题了,这是典型应用场景)

1.概述(四个关键点,布局方向,换行,主轴排列方式,辅轴排列方式)

弹性布局(Flex)提供更加有效的方式对容器中的子元素进行排列、对齐和分配剩余空间。常用于页面头部导航栏的均匀分布、页面框架的搭建、多行数据的排列等。

2.布局方向

在弹性布局中,容器的子元素可以按照任意方向排列。通过设置参数direction,可以决定主轴的方向,从而控制子元素的排列方向。

FlexDirection.Row(默认值):主轴为水平方向,子元素从起始端沿着水平方向开始排布。

Flex({ direction: FlexDirection.Row }) {
  Text('1').width('33%').height(50).backgroundColor(0xF5DEB3)
  Text('2').width('33%').height(50).backgroundColor(0xD2B48C)
  Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
}
.height(70)
.width('90%')
.padding(10)
.backgroundColor(0xAFEEEE)

**FlexDirection.Column:**主轴为垂直方向,子元素从起始端沿着垂直方向开始排布。

Flex({ direction: FlexDirection.Column }) {
  Text('1').width('100%').height(50).backgroundColor(0xF5DEB3)
  Text('2').width('100%').height(50).backgroundColor(0xD2B48C)
  Text('3').width('100%').height(50).backgroundColor(0xF5DEB3)
}
.height(70)
.width('90%')
.padding(10)
.backgroundColor(0xAFEEEE)

3.布局换行

弹性布局分为单行布局和多行布局。默认情况下,Flex容器中的子元素都排在一条线(又称“轴线”)上。wrap属性控制当子元素主轴尺寸之和大于容器主轴尺寸时,Flex是单行布局还是多行布局。在多行布局时,通过交叉轴方向,确认新行排列方向。

FlexWrap. NoWrap(默认值):不换行。如果子元素的宽度总和大于父元素的宽度,则子元素会被压缩宽度。

Flex({ wrap: FlexWrap.NoWrap }) {
  Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
  Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
  Text('3').width('50%').height(50).backgroundColor(0xF5DEB3)
} 
.width('90%')
.padding(10)
.backgroundColor(0xAFEEEE)

FlexWrap. Wrap:换行,每一行子元素按照主轴方向排列

Flex({ wrap: FlexWrap.Wrap }) {
  Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
  Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
  Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
} 
.width('90%')
.padding(10)
.backgroundColor(0xAFEEEE)

4.主轴对齐方式

通过justifyContent参数设置子元素在主轴方向的对齐方式

5.交叉轴对齐方式

​ 容器和子元素都可以设置交叉轴对齐方式,且子元素设置的对齐方式优先级较高。

​ 1.容器组件设置交叉轴对齐

​ ItemAlign.Auto:使用Flex容器中默认配置。

​ ItemAlign.Start:交叉轴方向首部对齐。

​ ItemAlign.Center:交叉轴方向居中对齐

let SWh:Record<string,number|string> = { 'width': '90%', 'height': 80 }
Flex({ alignItems: ItemAlign.Center }) {  
  Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
  Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
  Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
}
.size(SWh)
.padding(10)
.backgroundColor(0xAFEEEE)

​ ItemAlign.End:交叉轴方向底部对齐。

​ 2.子元素设置交叉轴对齐(上一个就可以实现)

场景示例

使用弹性布局,可以实现子元素沿水平方向排列,两端对齐,子元素间距平分,垂直方向上子元素居中的效果。

@Entry  
@Component
struct FlexExample {
  build() {
    Column() {
      Column({ space: 5 }) {
        Flex({ direction: FlexDirection.Row, wrap: FlexWrap.NoWrap, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
          Text('1').width('30%').height(50).backgroundColor(0xF5DEB3)
          Text('2').width('30%').height(50).backgroundColor(0xD2B48C)
          Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
        }
        .height(70)
        .width('90%')
        .backgroundColor(0xAFEEEE)
      }.width('100%').margin({ top: 5 })
    }.width('100%') 
 }
}

5.组件

1.使用文本

文本显示 (Text/Span)-使用文本-UI开发 (ArkTS声明式开发范式)-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者 (huawei.com)

1.文本显示(Text/Span)

a.常见属性

属性描述
fontSize字体大小
fontColor字体颜色
fontStyle字体样式
fontWeight字体粗细
lineHeight文本行高
decoration文本修饰线及颜色
textAlign水平方向Text对齐方式
align垂直方向对齐方式
textIndent文本首行缩进

b.通过textCase设置文字一直保持大写或者小写状态。

Text() {
  Span('I am Upper-span').fontSize(12)
    .textCase(TextCase.UpperCase)
}
.borderWidth(1)
.padding(10)

c.通过copyOption属性设置文本是否可复制粘贴。

Text("这是一段可复制文本")
  .fontSize(30)
  .copyOption(CopyOptions.InApp)

d.添加子组件

​ 创建Span。

Span组件需要写到Text组件内,单独写Span组件不会显示信息,Text与Span同时配置文本内容时,Span内容覆盖Text内容。

Text('我是Text') {
  Span('我是Span')
}
.padding(10)
.borderWidth(1)

e.场景示例

// 试一试吧
@Entry
@Component
struct TextExample {
  build() {
    Column() {
      Row() {
        Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
        Text("我是热搜词条1")
          .fontSize(12)
          .fontColor(Color.Blue)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .fontWeight(300)
        Text("爆")
          .margin({ left: 6 })
          .textAlign(TextAlign.Center)
          .fontSize(10)
          .fontColor(Color.White)
          .fontWeight(600)
          .backgroundColor(0x770100)
          .borderRadius(5)
          .width(15)
          .height(14)
      }.width('100%').margin(5)

      Row() {
        Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
        Text("我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2")
          .fontSize(12)
          .fontColor(Color.Blue)
          .fontWeight(300)
          .constraintSize({ maxWidth: 200 })
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
        Text("热")
          .margin({ left: 6 })
          .textAlign(TextAlign.Center)
          .fontSize(10)
          .fontColor(Color.White)
          .fontWeight(600)
          .backgroundColor(0xCC5500)
          .borderRadius(5)
          .width(15)
          .height(14)
      }.width('100%').margin(5)

      Row() {
        Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 })
        Text("我是热搜词条3")
          .fontSize(12)
          .fontColor(Color.Blue)
          .fontWeight(300)
          .maxLines(1)
          .constraintSize({ maxWidth: 200 })
          .textOverflow({ overflow: TextOverflow.Ellipsis })
        Text("热")
          .margin({ left: 6 })
          .textAlign(TextAlign.Center)
          .fontSize(10)
          .fontColor(Color.White)
          .fontWeight(600)
          .backgroundColor(0xCC5500)
          .borderRadius(5)
          .width(15)
          .height(14)
      }.width('100%').margin(5)

      Row() {
        Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 })
        Text("我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4")
          .fontSize(12)
          .fontColor(Color.Blue)
          .fontWeight(300)
          .constraintSize({ maxWidth: 200 })
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      }.width('100%').margin(5)
    }.width('100%')
  }
}

2.文本输入 (TextInput/TextArea)

a.创建输入框

TextInput为单行输入框、TextArea为多行输入框。通过以下接口来创建。

TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})

TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})

2.使用图片(Image)

1.概述

开发者经常需要在应用中显示一些图片,例如:按钮中的icon、网络图片、本地图片等。在应用中显示图片需要使用Image组件实现,Image支持多种图片格式,包括png、jpg、bmp、svg、gif和heif,具体用法请参考Image组件。

2.加载图片
Image(src: PixelMap | ResourceStr | DrawableDescriptor)

a.本地资源

创建文件夹,将本地图片放入ets文件夹下的任意位置。

Image组件引入本地图片路径,即可显示图片(根目录为ets文件夹)

Image('images/view.jpg')
.width(200)

b.网络资源

引入网络图片需申请权限ohos.permission.INTERNET,具体申请方式请参考声明权限。此时,Image组件的src参数为网络图片的链接。

当前Image组件仅支持加载简单网络图片。

Image组件首次加载网络图片时,需要请求网络资源,非首次加载时,默认从缓存中直接读取图片,更多图片缓存设置请参考setImageCacheCountsetImageRawDataCacheSizesetImageFileCacheSize。但是,这三个图片缓存接口并不灵活,且后续不继续演进,对于复杂情况,更推荐使用ImageKnife

Image('https://www.example.com/example.JPG') // 实际使用时请替换为真实地址

c.Resource资源

使用资源格式可以跨包/跨模块引入图片,resources文件夹下的图片都可以通过$r资源接口读 取到并转换到Resource格式。

//调用方式
Image($r('app.media.icon'))

还可以将图片放在rawfile文件夹下。

Image($rawfile('example1.png'))
3.显示矢量图

Image组件可显示矢量图(svg格式的图片),支持的svg标签为:svg、rect、circle、ellipse、path、line、polyline、polygon和animate。

svg格式的图片可以使用fillColor属性改变图片的绘制颜色。

Image($r('app.media.cloud'))
  .width(50)
  .fillColor(Color.Blue) 

原始图片

fillColor添加填充色后的图片

4.组件属性
属性描述
width宽度(通用属性)
height高度(通用属性)
aspectRatio宽高比(通用属性)
alt加载时显示的占位图,支持本地图片(png、jpg、bmp、svg和gif类型),不支持网络图片。
objectFit设置图片的填充效果。默认值:ImageFit.Cover

属性:objectFit

参数类型:枚举 ImageFit

  1. **Contain:**图片宽或高缩放到与组件尺寸相同则停止缩放,可能导致组件有空白(等比缩放)

  2. **Cover:**默认效果,图片缩放到完全覆盖组件范围,可能导致图片显示不完整(等比缩放)

  3. **Fill:**图片缩放至充满组件(不等比缩放)

    设置图片渲染模式

    通过renderMode属性设置图片的渲染模式为原色或黑白。

    @Entry
    @Component
    struct MyComponent {
      build() {
        Column({ space: 10 }) {
          Row({ space: 50 }) {
            Image($r('app.media.example'))
              // 设置图片的渲染模式为原色 
              .renderMode(ImageRenderMode.Original)
              .width(100)
              .height(100)
              .border({ width: 1 })
                // overlay是通用属性,用于在组件上显示说明文字
              .overlay('Original', { align: Alignment.Bottom, offset: { x: 0, y: 20 } })
            Image($r('app.media.example'))
              // 设置图片的渲染模式为黑白
              .renderMode(ImageRenderMode.Template)
              .width(100)
              .height(100)
              .border({ width: 1 })
              .overlay('Template', { align: Alignment.Bottom, offset: { x: 0, y: 20 } })
          }
        }.height(150).width('100%').padding({ top: 20,right: 10 })
      }
    }
    

:**默认效果,图片缩放到完全覆盖组件范围,可能导致图片显示不完整(等比缩放)

  1. **Fill:**图片缩放至充满组件(不等比缩放)

    设置图片渲染模式

    通过renderMode属性设置图片的渲染模式为原色或黑白。

    @Entry
    @Component
    struct MyComponent {
      build() {
        Column({ space: 10 }) {
          Row({ space: 50 }) {
            Image($r('app.media.example'))
              // 设置图片的渲染模式为原色 
              .renderMode(ImageRenderMode.Original)
              .width(100)
              .height(100)
              .border({ width: 1 })
                // overlay是通用属性,用于在组件上显示说明文字
              .overlay('Original', { align: Alignment.Bottom, offset: { x: 0, y: 20 } })
            Image($r('app.media.example'))
              // 设置图片的渲染模式为黑白
              .renderMode(ImageRenderMode.Template)
              .width(100)
              .height(100)
              .border({ width: 1 })
              .overlay('Template', { align: Alignment.Bottom, offset: { x: 0, y: 20 } })
          }
        }.height(150).width('100%').padding({ top: 20,right: 10 })
      }
    }
    

    [外链图片转存中…(img-i46VJJk7-1730857475476)]

    -----------------------------(第二次)今天更新结束觉得写的不错就点点关注吧❤---------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值