不加.layoutWeight(1)显示不全,加了.layoutWeight(1)如果列表项比较少,会被强制居中,怎么解决呢?

不加.layoutWeight(1)显示不全,加了.layoutWeight(1)如果列表项比较少,会被强制居中,怎么解决呢?

@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World'  
  @State arr:number[] = []  
  @State arr1:number[] = []  
  @State state:boolean = false  
  aboutToAppear(): void {  
  
    for (let i = 0; i < 10; i++) {  
      this.arr1.push(i)  
    }  
  }  
  private scrollerForScroll: Scroller = new Scroller()  
  build() {  
    Column() {  
      Text('切换')  
        .height(40)  
        .margin({top:50})  
        .width('100%').onClick(()=>{  
        this.state = !this.state  
      })  
      Scroll(this.scrollerForScroll) {  
        Column() {  
          if(this.state){  
            ForEach(this.arr, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Red)  
            }, (items: number) => items.toString())  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
  
          }else{  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
          }  
  
        }  
        .width('100%')  
      }.layoutWeight(1)  
    }  
    .height('100%')  
  }  
}
HarmonyOS
2024-09-23 14:06:43
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

使用Flex布局 demo如下:

@Entry  
@Component  
struct FlexLimitScollerHeightPage {  
  
  @State message: string = 'Hello World'  
  @State arr:number[] = []  
  @State arr1:number[] = []  
  @State state:boolean = false  
  aboutToAppear(): void {  
  
    for (let i = 0; i < 10; i++) {  
      this.arr1.push(i)  
    }  
  }  
  private scrollerForScroll: Scroller = new Scroller()  
  build() {  
    Flex({direction: FlexDirection.Column, justifyContent:FlexAlign.Start}) {  
      // Column() {  
      Text('切换 -- 标题')  
        .height(40)  
        .margin({top:50})  
        .width('100%').onClick(()=>{  
        this.state = !this.state  
      })  
      Scroll(this.scrollerForScroll) {  
        Flex({direction: FlexDirection.Column, justifyContent:FlexAlign.Start}) {  
          // Column() {  
          if(this.state){  
            ForEach(this.arr, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Red)  
            }, (items: number) => items.toString())  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString()).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString() + 22).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
          }else{  
            ForEach(this.arr1, (item: number) => {  
              Text(item.toString() + 22).height(40)  
              Divider().color(Color.Green)  
            }, (items: number) => items.toString())  
          }  
  
        }  
        .width('100%')  
      }  
    }  
    .height('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-09-23 17:26:31
相关问题
HarmonyOS list 列表项交换
26浏览 • 1回复 待解决
HarmonyOS 2in1怎么禁止最大化
39浏览 • 1回复 待解决
HarmonyOS web显示不全
555浏览 • 1回复 待解决
Text文本不垂直居中,该怎么处理
3164浏览 • 1回复 待解决
Scroll组件内显示不全问题
1187浏览 • 1回复 待解决
HarmonyOS Tabs 控件 底部显示不全
35浏览 • 1回复 待解决
neptune 烧写报错[upload] Error -1
6361浏览 • 2回复 待解决
HarmonyOS HmacSHA1加签算法实现
736浏览 • 1回复 待解决
如何获取HarmonyOS 项目的SHA1?
5262浏览 • 1回复 待解决