HarmonyOS 使用flex实现折行怎么判断行数和设置行数

不足两行的话显示更多按钮,点击更多按钮显示5行。使用Flex来实现的话,怎么判断当前行数和设置最大行数呢?或者有其他更好的实现方式吗?

HarmonyOS
2024-09-30 16:06:42
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

目前是通过控制Text的宽度和个数,用计算来实现行数的变化。可以参考一下:

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 Index {  
  @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[] = ['222222222222','22','333','44444','55','666','7777','8888888888888','99','3434','5656','7878','141414141','68681','哈哈哈','xixixi']  
  @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+5)  
    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 = '展开'  
                this.ShowData = this.SomeData  
              }  
            })  
            .width(80)  
            .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-09-30 17:45:05
相关问题
HarmonyOS如何使Text中的单词显示
370浏览 • 1回复 待解决
HarmonyOS 如何进行数据持久化
255浏览 • 1回复 待解决
HarmonyOS如何进行数据文件存储
306浏览 • 1回复 待解决
FA卡片如何进行数据交互
2092浏览 • 1回复 待解决
HarmonyOS Flex 布局设置问题
341浏览 • 1回复 待解决
PolarDB 如何进行数据通信?
2885浏览 • 1回复 待解决
手机如何与电脑端进行数据传输
3184浏览 • 1回复 待解决
TaskPool线程中如何进行数据库操作?
147浏览 • 1回复 待解决