HarmonyOS 两种模式布局如何兼容。

第一种布局:

是子控件高度确定,撑起父控件。

第二种布局:

是父控件的高度是由兄弟组件给出流出的余空间作为高度,子控件按照某种比例计以及当前父控件的高度算出各自高度。

现在通过一个变量,在同一个组件中来控制切换两种布局模式,应该如何做。

HarmonyOS
2024-08-29 15:48:41
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

可参考:

class MyModifier implements AttributeModifier<ColumnAttribute> { 
  isRemain: boolean = false 
 
  applyNormalAttribute(instance: ColumnAttribute): void { 
    if (this.isRemain) { 
      instance.layoutWeight(1) 
    } 
  } 
} 
@Entry 
@Component 
struct Index { 
  @State onPen: boolean = true 
  @State modifier: MyModifier = new MyModifier() 
  @State comp1Height: Length = 0 
  @State comp2Height: Length = 0 
  build() { 
    Column () { 
      Text('文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定文本数量不确定') 
      // onPen:true,希望cmp1和cmp2撑起外层Colum的高度这个比较好写 
      // false,希望colum高度是当前布局的剩余高度即Brown颜色部分。然后Cmp1和Cmp1的高度是Brown区域高度的2比1分配(这个在这个基础上怎么写) 
      Column() { 
        Cmp1() 
          .height(this.onPen ? this.getH1() : this.comp1Height) 
          .width('90%') 
          .backgroundColor('#ffe4aaf8') 
        Cmp2() 
          .height(this.onPen ? this.getH2() : this.comp2Height) 
          .width('90%') 
          .backgroundColor('#ff3094fa') 
      } 
      .width('100%') 
      .backgroundColor(Color.Brown) 
      .attributeModifier(this.modifier) 
      .onAreaChange((oldValue: Area, newValue: Area) => { 
        let height = newValue.height 
        this.comp1Height = Number(height) * (2/3) 
        this.comp2Height = Number(height) * (1/3) 
      }) 
  Text('点击切换模式') 
    .onClick(() => { 
      this.onPen = !this.onPen 
      this.modifier.isRemain = !this.modifier.isRemain 
    }) 
} 
.height('100%') 
.width('100%') 
.backgroundColor(Color.Green) 
 
  } 
  getH1(): number { 
    // true,希望cmp1和cmp2撑起外层Colum的高度 
    if (this.onPen) { 
      return 100 
    } else { 
    } 
    return 0 
  } 
  getH2(): number { 
    // true,希望cmp1和cmp2撑起外层Colum的高度 
    if (this.onPen) { 
      return 89 
    } else { 
    } 
    return 0 
  } 
} 
@Component 
struct Cmp1 { 
  build() { 
    Column() { 
      Text('1') 
    } 
  } 
} 
@Component 
struct Cmp2 { 
  build() { 
    Column() { 
      Text('2') 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
分享
微博
QQ
微信
回复
2024-08-29 16:59:47


相关问题
PolarDB 集群的连接地址包括哪两种
3327浏览 • 1回复 待解决
js获取canvas对象两种方式有啥不同?
8225浏览 • 1回复 待解决