Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
ContainerReader is a container breakpoint component used to obtain breakpoint information based on container size in dynamic scenarios and perform responsive layout. This component returns the container's size and breakpoint in real time through two-way binding, enabling you to create and lay out components based on container size.
Since: 26.0.0
Supported
ContainerReader(value: ContainerReaderInfo)
Creates a ContainerReader component and configures container reader parameters.
Since: 26.0.0
Model restriction: This API can be used only in the stage model.
Widget capability: This API can be used in ArkTS widgets since API version 26.0.0.
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | ContainerReaderInfo | Yes | Container reader configuration options, including size data and breakpoint configuration. |
Defines the configuration options for the ContainerReader component, used to specify parameters for reading container size and obtaining breakpoint values. The component size and breakpoint values cannot be changed through this parameter.
Since: 26.0.0
Model restriction: This API can be used only in the stage model.
Widget capability: This API can be used in ArkTS widgets since API version 26.0.0.
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| size | Size | No | No | Size of the ContainerReader component, used for layout analysis and breakpoint calculation. Note: This parameter supports two-way binding through !!. After binding, when the component size value changes, the bound variable of size automatically updates. |
| widthBreakpoint | WidthBreakpoint | No | Yes | Width breakpoint of the container, which is the obtained width breakpoint enum value of the ContainerReader component. Note: This parameter supports two-way binding through !!. After binding, when the component width breakpoint value changes, the bound variable of widthBreakpoint automatically updates. |
| heightBreakpoint | HeightBreakpoint | No | Yes | Height breakpoint of the container, which is the height breakpoint enum value of the ContainerReader component under different aspect ratio thresholds. Note This parameter supports two-way binding through !!. After binding, when the component height breakpoint value changes, the bound variable of heightBreakpoint automatically updates. |
In addition to the universal attributes, the following attributes are supported:
breakpointConfig(value?: BreakpointOptions)
Sets the breakpoint configuration options, defining the size thresholds that trigger different layout behaviors.
Since: 26.0.0
Model restriction: This API can be used only in the stage model.
Widget capability: This API can be used in ArkTS widgets since API version 26.0.0.
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | BreakpointOptions | No | Breakpoint configuration options, containing arrays of width and height breakpoint thresholds. |
Defines the breakpoint configuration options, which are used to specify threshold parameters for container size analysis.
Since: 26.0.0
Model restriction: This API can be used only in the stage model.
Widget capability: This API can be used in ArkTS widgets since API version 26.0.0.
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Type | Read-only | Optional | Description |
|---|---|---|---|---|
| width | Array<number> | No | Yes | Array of width breakpoint values. The array must be monotonically increasing. Default value: [320, 600, 840, 1440], in vp, consistent with the default window width breakpoints. Note: A maximum of 5 breakpoints are supported, meaning the maximum array length is 4. |
| height | Array<number> | No | Yes | Array of height breakpoint values. The height breakpoint value is the ratio of the component's height to its width. No unit. The array must be monotonically increasing. Default value: [0.8, 1.2], consistent with the default window height breakpoints. Note: A maximum of 3 breakpoints are supported, meaning the maximum array length is 2. |
Universal events are supported.
This example demonstrates how the ContainerReader component obtains container size and breakpoint information through two-way binding, and switches the layout direction based on the width breakpoint.
The ContainerReader component is added since API version 26.0.0.
To use ContainerReader, you need to import ContainerReaderAttribute at the same time, otherwise it will cause a compilation error.
// xxx.ets
import { ContainerReader, ContainerReaderAttribute, Size } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State containerSize: Size = { width: 0, height: 0 };
@State widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_XS;
@State heightBp: HeightBreakpoint = HeightBreakpoint.HEIGHT_SM;
@State columnWidth: number = 180
build() {
Column({space: 10}) {
Column({space: 10}) {
ContainerReader({
size: this.containerSize!!, // Bind to the this.containerSize variable. When the ContainerReader size changes, this.containerSize is automatically updated.
widthBreakpoint: this.widthBp!!,
heightBreakpoint: this.heightBp!!
}) {
// Switch layout direction based on the width breakpoint
if (this.widthBp === WidthBreakpoint.WIDTH_XS) {
Column({space: 20}) {
Text('Vertical layout')
Text(`Column`)
}
.width('100%')
.height('100%')
.backgroundColor('#D5D5D5')
.justifyContent(FlexAlign.Center)
} else {
Row({space: 20}) {
Text('Horizontal layout')
Text(`Row`)
}
.width('100%')
.height('100%')
.backgroundColor('#2787D9')
.justifyContent(FlexAlign.Center)
}
}
.backgroundColor('#F0FAFF')
}
.height('20%')
.width(this.columnWidth)
Button('Change column width')
.onClick(()=>{
if (this.columnWidth == 180) {
this.columnWidth = 320;
} else {
this.columnWidth = 180;
}
})
}
.height('100%')
.width('100%')
}
} This example demonstrates how to customize breakpoint thresholds via breakpointConfig to define various wide and narrow layout sizes, enabling more refined layout control.
The ContainerReader component and the breakpointConfig API are added since API version 26.0.0.
To use ContainerReader, you need to import ContainerReaderAttribute at the same time, otherwise a compilation error will occur.
// xxx.ets
import { ContainerReader, ContainerReaderAttribute, Size } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State containerSize: Size = { width: 0, height: 0 };
@State widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_XS;
@State heightBp: HeightBreakpoint = HeightBreakpoint.HEIGHT_SM;
@State columnWidth: number = 180
build() {
Column({space: 10}) {
Column({space: 10}) {
ContainerReader({
size: this.containerSize!!,
widthBreakpoint: this.widthBp!!,
heightBreakpoint: this.heightBp!!
}) {
if (this.widthBp === WidthBreakpoint.WIDTH_XS || this.widthBp === WidthBreakpoint.WIDTH_SM) {
Column({space: 10}) {
Text('Narrow screen layout')
.fontSize(16)
Text(`Width breakpoint: ${this.widthBp}`)
}
.width('100%')
.height('100%')
.backgroundColor('#D5D5D5')
.justifyContent(FlexAlign.Center)
} else {
Row({space: 10}) {
Text('Wide screen layout')
.fontSize(16)
Text(`Width breakpoint: ${this.widthBp}`)
}
.width('100%')
.height('100%')
.backgroundColor('#2787D9')
.justifyContent(FlexAlign.Center)
}
}
.height(100)
.width('100%')
.backgroundColor('#F0FAFF')
.breakpointConfig({ width: [100, 200, 400, 500], height: [0.8, 1.2] })
}
.height('20%')
.width(this.columnWidth)
Button('Change column width')
.onClick(()=>{
if (this.columnWidth == 180) {
this.columnWidth = 320;
} else {
this.columnWidth = 180;
}
})
}
.height('100%')
.width('100%')
}
} Tap the button to change the width of the parent container, which returns different width breakpoint values, thereby adjusting the layout direction.
This example demonstrates how to dynamically adjust the number of columns based on the width breakpoint obtained from ContainerReader, enabling adaptive layouts across multiple devices with varying column counts for different breakpoints.
The ContainerReader component is added since API version 26.0.0.
To use ContainerReader, you need to import ContainerReaderAttribute at the same time, otherwise a compilation error will occur.
// xxx.ets
import { ContainerReader, ContainerReaderAttribute, Size } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State containerSize: Size = { width: 0, height: 0 };
@State widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_XS;
@State columnWidth: number = 180
build() {
Column({space: 10}) {
Column({space: 10}) {
ContainerReader({
size: this.containerSize!!,
widthBreakpoint: this.widthBp!!
}) {
Row({ space: 2 }) {
if (this.widthBp === WidthBreakpoint.WIDTH_XS || this.widthBp === WidthBreakpoint.WIDTH_SM) {
Column() {
Text(`Column 1`)
.fontColor(Color.White)
}
.width('100%')
.height(60)
.backgroundColor('#D5D5D5')
.justifyContent(FlexAlign.Center)
.borderRadius(8)
.layoutWeight(1)
} else {
Column() {
Text(`Column 1`)
.fontColor(Color.White)
}
.width('100%')
.height(60)
.backgroundColor('#D5D5D5')
.justifyContent(FlexAlign.Center)
.borderRadius(8)
.layoutWeight(1)
Column() {
Text(`Column 2`)
.fontColor(Color.White)
}
.width('100%')
.height(60)
.backgroundColor('#707070')
.justifyContent(FlexAlign.Center)
.borderRadius(8)
.layoutWeight(1)
}
}
.width('100%')
.height('100%')
}
.breakpointConfig({ width: [100, 200, 400, 500] })
.backgroundColor('#F0FAFF')
}
.height('20%')
.width(this.columnWidth)
Button('Change column width')
.onClick(()=>{
if (this.columnWidth == 180) {
this.columnWidth = 320;
} else {
this.columnWidth = 180;
}
})
}
.height('100%')
.width('100%')
}
}