遇到这个问题的时候必须指定startPoint
和endPoint
属性才能画出直线,但是由于预览器中通过
let screenWidth = display.getDefaultDisplaySync().width
获取屏幕宽度会拿到空值0,所以预览器中预览界面非常不便,此时可以通过设置背景颜色和限制组件高度来实现类似效果:
@Extend(Line) function lineStyles(value: string = "100%"){
.width(value)
.height(1)
.backgroundColor(Color.Black)
}
let screenWidth = display.getDefaultDisplaySync().width
@Entry
@ComponentV2
struct AddTasks {
@Local message: string = 'Hello World';
build() {
Column() {
Line()
.width("90%").backgroundColor(Color.Black)
Button("log").onClick(() => {
console.log(`${screenWidth}`)
})
}
}
}
当然如果你只是想要一个类似其他组件库的输入框下划线,只要加上.showUnderline(true)
就行了。