HarmonyOS鸿蒙Next中Flex()组件怎么自适应子元素的宽高

HarmonyOS鸿蒙Next中Flex()组件怎么自适应子元素的宽高 我使用.width(‘auto’)去实现是可以了,但是加了wrap:FlexWrap.Wrap后.width(‘auto’)就会失效

@Entry
@Component
struct WrapExample {
  build() {
    Row() {
      Column() { // 新增Column层
        Flex({wrap:FlexWrap.Wrap}) {
          Text('子元素').width(100)
        }.flexGrow(0)
        .width('auto')
        .backgroundColor('red').width('auto')
      }
    }.backgroundColor('blue').height(200).width(200)
  }
}

更多关于HarmonyOS鸿蒙Next中Flex()组件怎么自适应子元素的宽高的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

Wrap:Flex容器的元素多行/列排布,子项允许超出容器。会导致auto属性失效。

当子组件的宽大于父组件的宽时,会溢出父组件的范围,可使用layoutWeight自适应布局,若遍历会影响其他子组件,可通过父组件宽度与文本宽度做比较判断是否自适应布局。

获取父组件宽度与文本宽度方案如下:

方案一:通过display.getDefaultDisplaySync获取屏幕宽度,MeasureText.measureText或onAreaChange获取文本宽度,判断文本宽是否大于屏幕宽,完成自适应布局。

主文件代码示例如下:

import { display, LengthMetrics, MeasureText } from '@kit.ArkUI'

@Component
@Entry
struct Index {
  options = ['这段很长的文字会溢出这段很长的文字会溢出这段很长的文字会溢出', '文字1', '文字2']

  build() {
    Flex({
      wrap: FlexWrap.Wrap,
      space: { main: LengthMetrics.vp(8), cross: LengthMetrics.vp(8) },
      direction: FlexDirection.Row,
      alignItems: ItemAlign.Start
    }) {
      ForEach(this.options, (item: string) => {
        ChildItem({ text: item })
      }, (item: string) => item)
    }
    .padding(10)
    .height(300)
  }
}

ChildItem子组件代码示例如下:

@Component
struct ChildItem {
  @Prop text: string = ""
  @State textWidth: number = 0
  @State totalWidth: number = 0

  aboutToAppear(): void {
    // 获取文本宽度
    this.textWidth = px2vp(MeasureText.measureText({
      textContent: this.text
    }))
    this.totalWidth = px2vp(display.getDefaultDisplaySync().width) // 获取屏幕宽度
  }

  build() {
    Row() {
      Checkbox({ name: this.text })
        .shape(CheckBoxShape.ROUNDED_SQUARE)
        .margin(0)
        .width(20)
        .height(20)
      Text(this.text)
        .backgroundColor(Color.Orange)
        .margin({
          left: 4
        })
        .layoutWeight(this.textWidth > this.totalWidth - 44 ? 1 : 0)
    }
    .height(50)
    .backgroundColor(Color.Blue)
  }
}

方案二:通过onAreaChange回调获取父组件宽度,MeasureText.measureText获取文本宽度,判断文本宽是否大于父组件宽,完成自适应布局。

核心代码示例如下:

@Component
struct ChildItem {
  @Prop text: string = ""
  @State textWidth: number = 0
  @State totalWidth: number = 0

  aboutToAppear(): void {
    // 获取文本宽度
    this.textWidth = px2vp(MeasureText.measureText({
      textContent: this.text
    }))
  }

  build() {
    Row() {
      Checkbox({ name: this.text })
        .shape(CheckBoxShape.ROUNDED_SQUARE)
        .margin(0)
        .width(20)
        .height(20)
      Text(this.text)
        .backgroundColor(Color.Orange)
        .margin({
          left: 4
        })
        .layoutWeight(this.textWidth - 24 > this.totalWidth ? 1 : 0)
    }
    .height(50)
    .backgroundColor(Color.Blue)
    // 获取父组件宽度
    .onAreaChange((oldValue: Area, newValue: Area) => {
      this.totalWidth = newValue.width as number
    })
  }
}

更多关于HarmonyOS鸿蒙Next中Flex()组件怎么自适应子元素的宽高的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


成功了,感觉回复楼主使用方案2实现了。我做的是类似微信气泡聊天的效果,通过判断父组件内容的宽度实现自适应。示例代码:

@Entry
@Component
struct Index {
  @State totalWidth: number = 0
  build() {
    Column(){
      Row {  // 外层约束容器
        Flex({wrap:this.totalWidth>320?FlexWrap.Wrap:FlexWrap.NoWrap}) {
          Text('内容我我我222')
          Image($r('app.media.face01')).width(80)
          Image($r('app.media.face01')).width(80)
          // Image($r('app.media.face01')).width(80)
        }
        .width('auto')  // 此时auto生效
        .backgroundColor('white')
        .padding(10)
        // 获取父组件宽度
        .onAreaChange(( oldValue: Area, newValue: Area ) => {
          this.totalWidth = newValue.width as number
          console.log(newValue.width.toString())
        })
      }
    }
    .alignItems(HorizontalAlign.Start)
    .height('100%')
    .width('100%')
    .backgroundColor('#ededed')
  }
}

在HarmonyOS鸿蒙Next中,Flex组件可以通过设置justifyContentalignItems属性来实现子元素的自适应宽高。默认情况下,Flex会根据子元素的内容自动调整其宽高。你可以使用FlexGrowFlexShrinkFlexBasis属性进一步控制子元素的自适应行为。例如,设置FlexGrow为1可以使子元素占据剩余空间,从而实现自适应布局。

回到顶部