目录

Responsive Grid Layout (GridRow/GridCol)

Overview

As an auxiliary positioning tool, the responsive grid layout is handy in UI design on mobile devices. It exhibits the following advantages:

  1. Provides rules for layout design and resolves issues of dynamic layout across devices with different sizes. By dividing a page into equal-width columns and rows, you can easily locate and typeset page elements.

  2. Provides a unified positioning method for the system to ensure layout consistency across layouts on different devices. This can reduce the complexity of design and development and improve work efficiency.

  3. Provides a flexible spacing adjustment method for applications to accommodate special layout requirements. You can adjust the spacing between columns and between rows to control the typesetting of the entire page.

  4. Automatic wrapping and adaptation: The grid layout can automatically wrap and adapt one-to-many layouts. When the number of page elements exceeds the capacity of a row or column, they automatically wrap to a new row or column and adapt the typesetting to different devices, making the page layout more flexible and adaptable.

The GridRow component is a responsive grid container and must have GridCol as its child component.

GridRow

[h2]Breakpoints

GridRow defines breakpoints, which are screen width types in effect, based on screen width (pixel unit, in vp). You can use the breakpoints to meet specific layout requirements.

By default, devices are categorized into four breakpoints.

Breakpoint Value Range (vp) Device Description
xs [0, 320) Minimum-width device.
sm [320, 600) Small-width device.
md [600, 840) Medium-width device.
lg [840, +∞) Large-width device.

You can customize breakpoints using the BreakPoints parameter, supporting up to six breakpoints: xs, sm, md, lg, xl, and xxl.

Breakpoint Device Description
xs Minimum-width device.
sm Small-width device.
md Medium-width device.
lg Large-width device.
xl Extra-large-width device.
xxl Extra-extra-large-width device.

[h2]Columns

The columns attribute defines the total number of columns in the GridRow container.

columns supports two types: number and GridRowColumnOption. You can set the total column count of the grid layout in two ways.

[h2]Alignment

In the grid layout, you can set the direction attribute of GridRow to define the direction in which child components are arranged. The options are GridRowDirection.Row (from left to right) or GridRowDirection.RowReverse (from right to left). An appropriate direction value can make the page layout more flexible and meet the design requirements.

[h2]Gutters

In the GridRow component, gutter is used to set the spacing between adjacent child components in the horizontal and vertical directions.

GridCol

The GridCol component is a child component of the GridRow component. You can set the span, offset, and order attributes of this component by passing parameters or using setters.

[h2]span

Sets the number of columns occupied by a child component in the grid layout, which determines the child component width. The default value is 1.

span supports two types: number and GridColColumnOption. You can set the number of columns that a grid subcomponent occupies within the grid container in two ways.

[h2]offset

Sets the column offset of a child component relative to the previous child component. The default value is 0.

[h2]order

The sequence number of a grid subcomponent, which determines the display order of subcomponents. When subcomponents have no order set or have the same order value, they are displayed in the order they appear in the code. When subcomponents have different order values, those with smaller values are displayed before those with larger values.

If order is set for only some child components, those with explicit order values are displayed after unordered components and sorted in ascending order.

Nesting of Responsive Grid Components

Responsive grid components can be contained in other responsive grid components.

In the following example, the responsive grid divides the entire space into 12 parts. At the first layer, GridCol is nested in GridRow, and the space is divided into the large area in the center and the footer area. At the second layer, GridCol is nested in GridRow, and the space is divided into the left and right areas. The child components take up the space allocated by the parent component at the upper layer. In this example, the pink area is made up of 12 columns of the screen space, and the green and blue areas take up the 12 columns of the parent component proportionally.

@Entry
@Component
struct GridRowExample {
  build() {
    GridRow({ columns: 12 }) {
      GridCol({ span: 12 }) {
        GridRow({ columns: 12 }) {
          GridCol({ span: 2 }) {
            Row() {
              Text('left').fontSize(24)
            }
            .justifyContent(FlexAlign.Center)
            .height('90%')
          }.backgroundColor('#ff41dbaa')

          GridCol({ span: 10 }) {
            Row() {
              Text('right').fontSize(24)
            }
            .justifyContent(FlexAlign.Center)
            .height('90%')
          }.backgroundColor('#ff4168db')
        }
        .backgroundColor('#19000000')
      }

      GridCol({ span: 12 }) {
        Row() {
          Text('footer').width('100%').textAlign(TextAlign.Center)
        }.width('100%').height('10%').backgroundColor(Color.Pink)
      }
    }.width('100%').height(300)
  }
}

To sum up, the responsive grid components are powerful tools with a wide range of customization capabilities. With the required attributes set at different breakpoints, such as columns, margin, gutter, gutter, and span, the layout is created automatically. You do not need to pay attention to the specific device type and device state (such as landscape and portrait).