基于ArkTS声明式开发范式,实现Flex、Column、Row和Stack四种常用布局容器对齐方式。
Access rate limit exceeded. Please log in to continue.
Common ArkUI Layout Container Alignment Modes
Introduction
This codelab introduces how to set the alignment modes of child components in the Flex, Column, Row, and Stack containers. You will learn to use alignItems, justifyContent, alignContent, alignSelf, and align to set alignment modes in different containers.
Effect

Concepts
- Main axis: There are two axes in a container by default, that is, the main axis and cross axis. The direction of the main axis varies according to the container.
- In the Column container, the direction of the main axis is vertical.
- In the Row container, the direction of the main axis is horizontal.
- In the Flex container, you can set the direction of the main axis by setting the direction parameter. If it is set to Column, the direction of the main axis is vertical. If it is set to Row, the direction of the main axis is horizontal.
- In the Stack container, the main axis and cross axis are not specified. You can set the alignContent parameter to change the alignment mode of components in the container.
- Cross axis: The cross axis intersects vertically with the main axis. If the main axis is vertical, the cross axis is horizontal, and vice versa.
Permissions
N/A
How to Use
- There are four container modules on the main page: Flex, Column, Row, and Stack. Tap a container module to go to the corresponding page.
- On the second page, there are different radio button lists for you to set different alignment modes.
Project Directory
├──entry/src/main/ets
│ ├── common
│ │ └── constants
│ │ └── CommonConstants.ets // Common constants
│ ├── entryability
│ │ └── EntryAbility.ets // Entry ability
│ ├── pages
│ │ ├── LayoutAlignIndex.ets // Sample homepage
│ │ └── Second.ets // Sample secondary page
│ ├── view
│ │ ├── ColumnAxisAlignRadioList.ets // Example of cross-axis alignment of the Column component
│ │ ├── ColumnMainAlignRadioList.ets // Example of the main axis alignment of the Column component
│ │ ├── ColumnShowList.ets // Example of the Column component alignment
│ │ ├── CommonComponent.ets // Common component
│ │ ├── FlexAxisAlignRadioList.ets // Example of cross-axis alignment for the Flex component
│ │ ├── FlexMainAlignRadioList.ets // Example of the main axis alignment of the Flex component
│ │ ├── FlexMainDirectionRadioList.ets // Example configuration of the spindle direction property of the Flex component
│ │ ├── FlexShowList.ets // Example of the Flex component alignment
│ │ ├── RowAxisAlignRadioList.ets // Example of cross-axis alignment of the Row component
│ │ ├── RowMainAlignRadioList.ets // Example of the main axis alignment of the Row component
│ │ ├── RowShowList.ets // Example of the Row component alignment
│ │ ├── StackAlignRadioList.ets // Example of the Stack component alignment
│ │ └── StackComponent.ets // Example of sub-components within the Stack component
│ └── viewmodel
│ ├── AttributeModuleData.ets // Attribute module data class
│ ├── ContainerModuleItem.ets // Spindle alignment data item class
│ ├── IndexData.ets // Home page data class
│ └── IndexListItem.ets // Home page data list item class
└──entry/src/main/resources // resources
How to Implement
Launch the application and click on the entry points of each sample page on the home page to enter different sample pages. On each sample page, select the properties radio box at the bottom to set different alignment methods. The display effect of the current alignment method will be updated synchronously at the top.
Constraints
- The sample is only supported on Bar phones with standard systems.
- HarmonyOS: HarmonyOS 26.0.0 Release or later.
- DevEco Studio: DevEco Studio 26.0.0 Release or later.
- HarmonyOS SDK: HarmonyOS 26.0.0 Release SDK or later.