HarmonyOS 鸿蒙Next中栅格布局GridCol这里有办法可以对齐吗?

HarmonyOS 鸿蒙Next中栅格布局GridCol这里有办法可以对齐吗? GridRow这个的GridCol按照order排完之后,这个3的位置可以通过设置调整上去吗?我翻找了官方的文档中,好像并没有相应的属性可以设置,AligItem.Start也无法取消这个空隙。

cke_2272.png


更多关于HarmonyOS 鸿蒙Next中栅格布局GridCol这里有办法可以对齐吗?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

当前栅格布局中确实没有很直接的方式来解决这个对齐的问题,这里只能自己手动适配
通过监听当前断点的类型变化,来讲对应的GridCol进行offset偏移
请参考代码:

// xxx.ets
@Entry
@Component
struct GridRowExample {
  @State currentBp: string = 'unknown'

  build() {
    Column() {
      GridRow({
        columns: 2,
        gutter: { x: 5, y: 10 },
        breakpoints: {
          value: ["400vp", "600vp", "800vp"],
          reference: BreakpointsReference.WindowSize
        },
        direction: GridRowDirection.Row
      }) {
        //1
        GridCol({
          span: {
            xs: 1,
            sm: 2
          },
          offset: 0,
          order: 1
        }) {
          Row()
            .width("100%").height("20vp")
        }
        .borderColor(Color.Red)
        .borderWidth(2)

        //2
        GridCol({
          span: {
            xs: 1,
            sm: 2
          },
          offset: 0,
          order: 2
        }) {
          Row().width("100%").height("60vp")
        }.borderColor(Color.Yellow).borderWidth(2)

        //3
        GridCol({
          span: {
            xs: 1,
            sm: 2
          },
          offset: 0,
          order: 3
        }) {
          Row().width("100%").height("20vp")
        }.borderColor(Color.Green).borderWidth(2)
        .offset({ y: this.currentBp == 'xs' ? -40 : 0 })

        //4
        GridCol({
          span: {
            xs: 1,
            sm: 2
          },
          offset: 0,
          order: 4
        }) {
          Row().width("100%").height("40vp")
        }.borderColor(Color.Blue).borderWidth(2)
      }.width("100%").height("100%")
      //.alignItems(ItemAlign.Start)
      .onBreakpointChange((breakpoint) => {
        console.log('hmLog-->', breakpoint)
        this.currentBp = breakpoint
      })
    }
    .width('80%')
    .margin({ left: 10, top: 5, bottom: 5 })
    .height(200)
    .border({ color: '#880606', width: 2 })
    .backgroundColor('#000')
  }
}

更多关于HarmonyOS 鸿蒙Next中栅格布局GridCol这里有办法可以对齐吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,GridCol组件支持通过alignment属性实现对齐。可设置值为GridRowAlign.Start、Center、End等,控制列内元素在垂直方向的对齐方式。同时,GridCol支持offset属性调整水平偏移量,结合GridRow的justifyContent属性可实现整体布局对齐。

在HarmonyOS Next中,GridCol的排列顺序由order属性控制,但默认布局遵循栅格系统的流式顺序,无法通过简单属性调整单个列的位置(例如将第3列移到上方)。若需调整布局结构,建议通过重新规划order值的分配或结合GridRowcolumns定义整体结构来实现对齐,避免依赖空隙调整属性。目前官方文档未提供直接修改单个列位置的功能。

回到顶部