DEV Community

Cover image for Lesson 1: HarmonyOS Next Layout Development Guide
liu yang
liu yang

Posted on

Lesson 1: HarmonyOS Next Layout Development Guide

Lesson 1: HarmonyOS Next Layout Development Guide

I. Core Concepts of Layout Containers

In HarmonyOS Next, ArkUI 5.0 offers efficient multi - device adaptation via a declarative layout engine. Here are three key layout containers:

Container Layout Direction Typical Scenarios
Column Vertical (Y - axis) Forms, vertical lists, info cards
Row Horizontal (X - axis) Nav bars, horizontal scrolling, button groups
Grid 2D grid (rows + columns) Albums, product displays, dashboards

II. Column Container: Mastering Vertical Layouts

1. Basic Usage and Attributes

Column() {  
  Text('Title').fontSize(20)  
  Text('Subtitle').fontColor('#666')  
  Button('Confirm').width(100)  
}  
.height('100%')  
.backgroundColor('#FFF')  
.padding(20)
Enter fullscreen mode Exit fullscreen mode

Key Attributes:

  • space: Child component spacing (e.g., .space(10) for 10px vertical spacing).
  • alignItems: Cross - axis alignment (HorizontalAlign.Start/Center/End).

2. Nested Layout Example

Column() {  
  // Top navigation bar  
  Row() { ... }  

  // Content area  
  Scroll() {  
    Column() {  
      Image($r('app.media.banner'))  
      Text('Main content...')  
    }  
  }  

  // Bottom buttons  
  Row() { ... }  
}
Enter fullscreen mode Exit fullscreen mode

III. Row Container: Advanced Horizontal Layouts

1. Adaptive Width Control

Row() {  
  Text('Label').flexGrow(1)  // Takes remaining space  
  Button('Action').flexShrink(0) // Can't be compressed  
}  
.justifyContent(FlexAlign.SpaceBetween)
Enter fullscreen mode Exit fullscreen mode

Flexible Layout Attributes:

  • flexGrow: Expansion ratio.
  • flexShrink: Contraction ratio.
  • flexBasis: Base size.

2. Complex Scenario: Scrollable Tab Bar

Scroll() {  
  Row() {  
    ForEach(this.tabs, (tab) => {  
      Text(tab.name)  
        .padding(10)  
        .borderRadius(15)  
    })  
  }  
}  
.scrollable(ScrollDirection.Horizontal)
Enter fullscreen mode Exit fullscreen mode

IV. Grid Container: In - Depth Grid Layouts

1. Basic Grid Definition

Grid() {  
  ForEach(this.items, (item) => {  
    GridItem() {  
      Image(item.url)  
    }  
  })  
}  
.columnsTemplate('1fr 1fr 1fr') // Three equal - width columns  
.rowsGap(15)  
.columnsGap(10)
Enter fullscreen mode Exit fullscreen mode

Parameter Explanation:

  • columnsTemplate: Column width definition (supports repeat(3, '1fr') shorthand).
  • rowsTemplate: Row height definition.

2. Responsive Grid Adaptation

Grid() {  
  // ...  
}  
.breakpoint({  
  'width < 600px': { columnsTemplate: '1fr' },    // Single column for phones  
  'width >= 600px': { columnsTemplate: '1fr 1fr' } // Two columns for tablets  
})
Enter fullscreen mode Exit fullscreen mode

V. Layout Alignment and Spacing System

1. Full Alignment Options

Main - axis Alignment (justifyContent):

Column() { ... }  
.justifyContent(FlexAlign.Center) // Center on main axis
Enter fullscreen mode Exit fullscreen mode

Options:

  • Start (default): Align to start.
  • Center: Center alignment.
  • End: Align to end.
  • SpaceBetween: Align to both ends.
  • SpaceAround: Equal spacing.

Cross - axis Alignment (alignItems):

Row() { ... }  
.alignItems(VerticalAlign.Center) // Vertical centering
Enter fullscreen mode Exit fullscreen mode

2. Spacing Control Solutions

Global Spacing System:

// Use preset spacing variables  
Column() { ... }  
.padding($r('app.float.page_margin')) // From resource file
Enter fullscreen mode Exit fullscreen mode

Custom Spacing:

Row() { ... }  
.margin({ top: 10, bottom: 20 })  
.width('100%')
Enter fullscreen mode Exit fullscreen mode

VI. Debugging and Optimization Tips

1. Visualizing Layout Boundaries

Enable debug mode in DevEco Studio:

Column() { ... }  
.debugLine(true) // Show layout borders (only in dev environment)
Enter fullscreen mode Exit fullscreen mode

2. Performance Optimization Tricks

  • Avoid nesting layouts beyond 3 levels.
  • Use FlexLayout instead of complex Row/Column combinations.
  • Use LazyForEach for long lists to load items on - demand.

Top comments (0)