HarmonyOS 鸿蒙Next当设置Scroll时页面无法上下居中
HarmonyOS 鸿蒙Next当设置Scroll时页面无法上下居中
当设置Scroll,但是当高度内容没铺满整个页面的时候,他是上下居中,而不是从上往下,就算给Scroll添加了高度100%也不起作用
当你删除掉轮播组件的时候,整个页面变成了上下居中的样式,这样是不符合逻辑的!如果你在Scroll的Column里面设置了高度100%,这样没铺满的页面是正常了,但是当你需要滑动的时候,你却没办法滑动了
/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
import CommonConstants from '../common/constants/CommonConstants';
import mainViewModel from '../viewmodel/MainViewModel';
import ItemData from '../viewmodel/ItemData';
/** * Home tab content */
@Component
export default struct Home {
private swiperController: SwiperController = new SwiperController();
build() {
Scroll() {
Column({ space: CommonConstants.COMMON_SPACE }) {
Column() {
Text($r('app.string.mainPage_tabTitles_home'))
.fontWeight(FontWeight.Medium)
.fontSize($r('app.float.page_title_text_size'))
.margin({ top: $r('app.float.mainPage_tabTitles_margin') })
.padding({ left: $r('app.float.mainPage_tabTitles_padding') })
}.width(CommonConstants.FULL_PARENT).alignItems(HorizontalAlign.Start)
Swiper(this.swiperController) {
ForEach(mainViewModel.getSwiperImages(), (img: Resource) => {
Image(img).borderRadius($r('app.float.home_swiper_borderRadius'))
}, (img: Resource) => JSON.stringify(img.id))
}.margin({ top: $r('app.float.home_swiper_margin') }).autoPlay(true)
Grid() {
ForEach(mainViewModel.getFirstGridData(), (item: ItemData) => {
GridItem() {
Column() {
Image(item.img).width($r('app.float.home_homeCell_size')).height($r('app.float.home_homeCell_size'))
Text(item.title)
.fontSize($r('app.float.little_text_size'))
.margin({ top: $r('app.float.home_homeCell_margin') })
}
}
}, (item: ItemData) => JSON.stringify(item))
}
.columnsTemplate('1fr 1fr 1fr 1fr')
.rowsTemplate('1fr 1fr')
.columnsGap($r('app.float.home_grid_columnsGap'))
.rowsGap($r('app.float.home_grid_rowGap'))
.padding({ top: $r('app.float.home_grid_padding'), bottom: $r('app.float.home_grid_padding') })
.height($r('app.float.home_grid_height'))
.backgroundColor(Color.White)
.borderRadius($r('app.float.home_grid_borderRadius'))
Text($r('app.string.home_list'))
.fontSize($r('app.float.normal_text_size'))
.fontWeight(FontWeight.Medium)
.width(CommonConstants.FULL_PARENT)
.margin({ top: $r('app.float.home_text_margin') })
Grid() {
ForEach(mainViewModel.getSecondGridData(), (secondItem: ItemData) => {
GridItem() {
Column() {
Text(secondItem.title).fontSize($r('app.float.normal_text_size')).fontWeight(FontWeight.Medium)
Text(secondItem.others)
.margin({ top: $r('app.float.home_list_margin') })
.fontSize($r('app.float.little_text_size'))
.fontColor($r('app.color.home_grid_fontColor'))
}.alignItems(HorizontalAlign.Start)
}
.padding({ top: $r('app.float.home_list_padding'), left: $r('app.float.home_list_padding') })
.borderRadius($r('app.float.home_backgroundImage_borderRadius'))
.align(Alignment.TopStart)
.backgroundImage(secondItem.img)
.backgroundImageSize(ImageSize.Cover)
.width(CommonConstants.FULL_PARENT)
.height(CommonConstants.FULL_PARENT)
}, (secondItem: ItemData) => JSON.stringify(secondItem))
}
.width(CommonConstants.FULL_PARENT)
.height($r('app.float.home_secondGrid_height'))
.columnsTemplate('1fr 1fr')
.rowsTemplate('1fr 1fr')
.columnsGap($r('app.float.home_grid_columnsGap'))
.rowsGap($r('app.float.home_grid_rowGap'))
.margin({ bottom: $r('app.float.setting_button_bottom') })
}
}.scrollBar(BarState.Off).height(CommonConstants.FULL_PARENT)
}
}
[@Component](/user/Component)
struct Home {
private swiperController: SwiperController = new SwiperController();
FirstGridData = [$r(‘app.media.1’), $r(‘app.media.2’), $r(‘app.media.3’), $r(‘app.media.4’)]
SecondGridData: Record<string, string>[] = [{ ‘title’: ‘1’ }, { ‘title’: ‘2’ }, { ‘title’: ‘3’ }, { ‘title’: ‘4’ }]
build() {
Scroll() {
Column({ space: 15 }) {
Column() {
Text(‘mainPage_tabTitles_home’)
.fontWeight(FontWeight.Medium)
.fontSize(14)
.margin({ top: 10 })
.padding({ left: 10 })
}
.width(‘100%’)
.alignItems(HorizontalAlign.Start)
Grid() {
ForEach(this.FirstGridData, (item: ESObject) => {
GridItem() {
Column() {
Image(item)
.width(80)
.height(80)
Text(item.title)
.fontSize(16)
.margin({ top: 10 })
}
}
}, (item: ESObject) => JSON.stringify(item))
}
.columnsTemplate(‘1fr 1fr 1fr 1fr’)
.rowsTemplate(‘1fr 1fr’)
.columnsGap(5)
.rowsGap(5)
.padding({ top: 10, bottom: 10 })
.height(100)
.backgroundColor(Color.White)
.borderRadius(10)
Text(‘home_list’)
.fontSize(14)
.fontWeight(FontWeight.Medium)
.width(‘100%’)
.margin({ top: 10 })
Grid() {
ForEach(this.SecondGridData, (secondItem: ESObject) => {
GridItem() {
Column() {
Text(secondItem.title)
.fontSize(14)
.fontWeight(FontWeight.Medium)
Text(secondItem.others)
.margin({ top: 10 })
.fontSize(16)
.fontColor(Color.Grey)
}
.alignItems(HorizontalAlign.Start)
}
.padding({ top: 10, left: 10 })
.borderRadius(5)
.align(Alignment.TopStart)
// .backgroundImage(secondItem.img)
// .backgroundImageSize(ImageSize.Cover)
.backgroundColor(’#eca’)
.width(‘100%’)
.height(‘100%’)
}, (secondItem: ESObject) => JSON.stringify(secondItem))
}
.width(‘100%’)
.height(300)
.columnsTemplate(‘1fr 1fr’)
.rowsTemplate(‘1fr 1fr’)
.columnsGap(5)
.rowsGap(5)
.margin({ bottom: 10 })
}
.constraintSize({minHeight:‘100%’})
}
.scrollBar(BarState.Off)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
针对HarmonyOS鸿蒙Next在设置Scroll时页面无法上下居中的问题,可以尝试以下解决方案:
首先,检查Scroll组件内嵌套的容器组件(如Column或Row)是否设置了height或width属性,因为在鸿蒙开发中,这些容器组件通常不应设置这两个属性,否则可能会影响内容的默认居中显示。
其次,可以利用align属性来设置Scroll组件内内容的对齐方式。例如,如果希望内容在Scroll组件内顶部左对齐,可以设置align属性为Alignment.TopStart。但请注意,如果希望内容居中,通常不需要显式设置align属性,因为Scroll组件在内容未超出显示区域时会默认居中显示。
最后,如果上述方法均无法解决问题,可能是Scroll组件的其他属性或页面布局设置影响了居中效果。此时,建议检查并调整相关属性,或尝试重新布局页面。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。