通过设置backgroundImageSize属性,限制背景图片尺寸,避免因页面尺寸变化导致背景图片显示异常。若不同的页面尺寸需要显示不同尺寸的背景图片,可以配合断点进行设置。
示例代码如下:
@Entry
@Component
struct ImageStretch {
bgImage: Resource = $r('app.media.backgroundcolorgray'); // 背景图更换为实际图片
build() {
Scroll() {
Column() {
Column() {
TextInput({ placeholder: 'Account' })
.width('95%')
.borderColor(Color.Gray)
.borderWidth(0.5)
.backgroundColor(Color.White)
.margin({ bottom: 10 });
TextInput({ placeholder: 'Password' })
.width('95%')
.borderColor(Color.Gray)
.borderWidth(0.5)
.backgroundColor(Color.White);
}
.height('60%')
.justifyContent(FlexAlign.End)
.margin({ bottom: 20 });
Button('Login')
.type(ButtonType.Capsule)
.width('60%');
}
.height('100%')
.justifyContent(FlexAlign.Start);
}
.height('100%')
.width('100%')
.backgroundImage(this.bgImage)
// 设置backgroundImageSize属性,限制背景图片尺寸
.backgroundImageSize({ height: 258, width: '100%' })
.expandSafeArea([SafeAreaType.SYSTEM]);
}
}