目录

RichEditor组件高度限制失败

问题现象

在父组件Column设置了尺寸限制,但是却未能在子组件RichEditor中生效,当给RichEditor输入多行文本后,RichEditor与其他子组件的总高度会超过父组件的尺寸限制。

问题代码示例参考如下:

@Entry
@Component
struct Question {
  richEditorController: RichEditorController = new RichEditorController();


  build() {
    Column() {
      Row().width('100%').height(50).backgroundColor('#87ceeb')
      RichEditor({ controller: this.richEditorController })
        .backgroundColor('#f8f8f8')
        .width('100%')
    }
    .width(200)
    .constraintSize({
      maxHeight: 100
    })
  }
}

背景知识

问题定位

检查父组件constraintSize是否可以对子组件进行有效限制:当子元素高度小于父组件高度限制时,父组件高度为设置的最小高度;子元素高度大于父组件高度限制时,父组件高度随子元素高度自适应。

分析结论

当前代码未对子组件进行有效的高度限制,需要添加子组件的高度限制属性。

修改建议

对子组件进行高度限制的方法如下:为子组件添加constraintSize。

import { window } from '@kit.ArkUI';


@Entry
@Component
struct RichEditorDemo {
  richEditorController: RichEditorController = new RichEditorController();


  aboutToAppear() {
    window.getLastWindow(this.getUIContext().getHostContext(), (err, win) => {
      if (err.code) {
        return;
      }
      win.setWindowLayoutFullScreen(true);
    });
  }


  build() {
    Column({ space: 16 }) {
      Row() {
        Text('此处用于对比50的高度')
      }
      .width('100%')
      .height(50)
      .backgroundColor('#ffffff')
      .justifyContent(FlexAlign.Center)
      .borderRadius(16)




      RichEditor({ controller: this.richEditorController })
        .backgroundColor('#ffffff')
        .width('100%')
        // 添加constraintSize属性,将高度限制为50,子组件高度相加不超过100(父组件高度限制)
        .constraintSize({ maxHeight: 50 })
        .borderRadius(16)
    }
    .width('100%')
    .height('100%')
    .padding(16)
    .backgroundColor('#f1f3f5')
    .margin({ top: 40 })
    // 添加constraintSize属性,将高度限制为148(50+50+32+16),子组件高度相加不超过100(父组件高度限制)
    .constraintSize({ maxHeight: 148 })
  }
}