HarmonyOS 如何限制Flex的行数

接口返回9个标签具体内容不知,但是UI要求只能显示一行(显示部分),使用Flex实现代码如下:

Flex({ wrap: FlexWrap.Wrap }) {
  ForEach(this.parentEncy?.dimensionList, (item: EncyDimension, index: number) => {
    Text(item.title)
      .fontSize($r('app.string.lpx_22'))
      .fontColor(index == 0 ? $r('app.color.white') : $r('app.color.theme_color'))
      .height($r('app.string.lpx_48'))
      .padding({ left: $r('app.string.lpx_24'), right: $r('app.string.lpx_24') })
      .borderRadius($r('app.string.lpx_24'))
      .borderColor($r('app.color.theme_color'))
      .borderWidth(index == 0 ? 0 : $r('app.string.lpx_1'))//设置渐变
      .linearGradient({
        direction: GradientDirection.Right,
        colors: index == 0 ? [[$r('app.color.theme_color'), 0.0], [$r('app.color.green_65CE3B'), 1.0]] :
          [[$r('app.color.transparent'), 0], [$r('app.color.transparent'), 1.0]]
      })
      .margin({ right: $r('app.string.lpx_24'), top: $r('app.string.lpx_22') })
      .onClick(() => {
        UriNavigator.openUri(this.getUIContext(), item.linkUrl)
      })
  }, (item: EncyDimension) => item.title)
}.visibility(this.parentEncy?.dimensionList?.length ?? 0 >= 2 ? Visibility.Visible : Visibility.None)

运行效果如图所示:

HarmonyOS 如何限制Flex的行数 -鸿蒙开发者社区

期望只显示一行即显示3个标签即可,如何实现?

HarmonyOS
2024-12-25 13:53:33
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考示例如下:

import measure from '@ohos.measure'
import display from '@ohos.display';

const childMaxWidth: number = 325 //为了方便后续计算,这里的宽度数值为px
let displayClass: display.Display | null = null;
let componentWidth: number = 0;
try {
  displayClass = display.getDefaultDisplaySync();
  componentWidth = displayClass.width
} catch (exception) {
  console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
}

@Component
struct TextItem {
  @State message: string = ''
  @Prop fontSizeData: number

  build() {
    Text(this.message)
      .fontSize(this.fontSizeData)
      .margin({ left: 10, top: 10 })
      .backgroundColor('#c4c2cc')
      .constraintSize({ maxWidth: childMaxWidth + 'px' })
      .maxLines(1)
      .textOverflow({ overflow: TextOverflow.Ellipsis })
  }
}

@Entry
@Component
struct Index2 {
  @State @Watch('IndexChange') index: number = 0
  @State @Watch('textChange') message: string = ''
  @State FlexWidth: string = '80%';
  @State newIndex: number = 0;
  @State heightControl: number | string = 100;
  @State ButtonText: string = '∨';
  @State AllData: string[] =
    ['1', '22', '511', '623', '55', '666', '648', '589', '99', '3434', '5656', '7878', '659',
      '68681']
  @State SomeData: string[] = []
  @State ShowData: string[] = []
  @State fontSizeData: number = 30
  @State AllWidth: number = 0
  @State textWidth: number = 0
  @State restrictWidth: number = 0;

  IndexChange() {
    if (this.AllWidth >= (this.restrictWidth - childMaxWidth) && this.AllWidth <= (this.restrictWidth)) {
      this.newIndex = this.index
      console.log('text1 newIndex', this.newIndex)
      console.log('text1 change', this.newIndex)
    }
  }

  textChange() {
    let content: string = this.message
    this.textWidth = measure.measureText({
      textContent: content,
      fontSize: this.fontSizeData
    })
    if (this.textWidth > childMaxWidth) {
      this.AllWidth += childMaxWidth
    } else {
      this.AllWidth += this.textWidth
    }
    console.log('text1 content', content)
    console.log('text1 Width', this.textWidth)
  }

  aboutToAppear(): void {
    if (componentWidth != 0) {
      this.restrictWidth = Math.floor((parseFloat(this.FlexWidth) * componentWidth) * 1.3 * 0.01)
      console.log('text1 componentWidth', componentWidth)
      console.log('text1 restrictWidth', this.restrictWidth)
    }
    for (let i = 0; i < this.AllData.length; i++) {
      this.message = this.AllData[i]
      this.index = i
    }
    console.log('text1 change newIndex', this.newIndex)
    this.SomeData = this.AllData.slice(0, this.newIndex + 1)
    this.ShowData = this.SomeData
  }

  build() {
    Row() {
      Column() {
        Flex({ wrap: FlexWrap.Wrap }) {
          ForEach(
            this.ShowData,
            (item: string) => {
              TextItem({ message: item, fontSizeData: this.fontSizeData })
            }
          )
          Button(this.ButtonText)
            .onClick(() => {
              if (this.heightControl === 100) {
                this.heightControl = '100%'
                this.ButtonText = '^'
                this.ShowData = this.AllData
              } else {
                this.heightControl = 100
                this.ButtonText = 'v'
                this.ShowData = this.SomeData
              }
            })
            .width(40)
            .height(30)
            .margin({ left: 10, top: 10 })
        }
        .constraintSize({ maxHeight: this.heightControl })
        .border({ width: 1 })
        .width(this.FlexWidth)
        .margin({ left: '5%' })
        .clip(true)
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-25 15:56:49
相关问题
HarmonyOS Flex布局如何设置最大行数
319浏览 • 1回复 待解决
HarmonyOS flex居中
260浏览 • 1回复 待解决
HarmonyOS Flex 组件组合使用
62浏览 • 1回复 待解决
如何优化Flex布局性能
687浏览 • 1回复 待解决
HarmonyOS flex容器布局
1184浏览 • 2回复 待解决
HarmonyOS 如何限制TextInput规则?
688浏览 • 1回复 待解决
如何阻止Flex容器鼠标事件穿透
2238浏览 • 1回复 待解决
HarmonyOS Text获取展示行数
50浏览 • 1回复 待解决
Flex布局与w3c中flex是否有差异
1200浏览 • 1回复 待解决
HarmonyOS Text组件如何计算文本行数
196浏览 • 1回复 待解决
HarmonyOS Flex组件是否有间隔属性
199浏览 • 1回复 待解决
HarmonyOS Flex组件存在问题,UI不对
201浏览 • 1回复 待解决