Divider组件能够分隔不同内容块或内容元素,可以在视觉上界定内容区域和引导用户视线移动,例如,使用垂直虚线分隔列,可以使用户更清晰地查看每一列的内容,同时保持整体的简洁感。Divider除通用属性外,还支持以下四个属性:
border属性仅能设置间隔长度均匀的虚线,如果需要绘制间隔长度不均匀的虚线,需要Canvas实现。
Canvas(this.context) .width('100%') .height('100%') .onReady(() => { this.context.lineWidth = 1; this.context.strokeStyle = 'black'; // 用户可在这自定义虚线的样式 this.context.setLineDash([10, 20, 30, 40, 50, 40, 30, 20, 10]); this.wrapText(0, 40, this.context.width); });
this.context.setLineDash([10, 20, 30, 40, 50, 40, 30, 20, 10]);
wrapText(x: number, y: number, maxWidth?: number) { this.context.beginPath(); this.context.moveTo(x, y + 10); this.context.lineTo(maxWidth, y + 10); this.context.stroke(); this.context.closePath(); }
@Entry @Component struct DashedLine { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); wrapText(x: number, y: number, maxWidth?: number) { this.context.beginPath(); this.context.moveTo(x, y + 10); this.context.lineTo(maxWidth, y + 10); this.context.stroke(); this.context.closePath(); } build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Canvas(this.context) .width('100%') .height('100%') .onReady(() => { this.context.lineWidth = 1; this.context.strokeStyle = 'black'; // 用户可在这自定义虚线的样式 this.context.setLineDash([10, 20, 30, 40, 50, 40, 30, 20, 10]); this.wrapText(0, 40, this.context.width); }); } .width('100%') .height('100%'); } }