harmony 鸿蒙Declarative UI Development Guidelines
Declarative UI Development Guidelines
Overview
Task | Description | Related Resources |
---|---|---|
Set up the development environment. | Understand the project structure of the declarative UI. Learn the resource categories and how to access resources. |
OpenHarmony Project Overview Resource Categories and Access |
Learn ArkTS. | As its name implies, ArkTS is a superset of TypeScript. It is the preferred, primary programming language for application development in OpenHarmony. | Learning ArkTS |
Develop a page. | Select an appropriate layout based on the use case. Add built-in components and set the component style based on the page content to present. Update and diversify the page content. |
Creating a Page Common Layout Development Common Components Setting the Component Style Updating Page Content |
(Optional) Diversify the page content. | Leverage the drawing and animation features to effectively increase user engagement. | Drawing Components Canvas Components Animation |
(Optional) Implement page redirection. | Use page routing to implement redirection between pages. | Page Routing |
(Optional) Improve performance. | Learn how you can improve your implementation, thereby avoiding possible performance drop. | Recommendations for Improving Performance |
Creating a Page
Select a layout to create a page and add basic built-in components to the page. In this example, the flex layout is used, and the <Text> component is centered horizontally and vertically on the page.
// xxx.ets
@Entry
@Component
struct MyComponent {
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('Hello')
}
.width('100%')
.height('100%')
}
}
Setting the Component Style
If a built-in component does not have attribute methods set, it takes the default style. To change the style of a component and thereby how it looks on the UI, modify the settings of styles, including component-specific styles and universal styles.
- Change the display content of the <Text> component to Tomato by modifying its constructor parameters.
- Set the fontSize attribute to 26 and the fontWeight attribute to 500.
// xxx.ets
@Entry
@Component
struct MyComponent {
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('Tomato')
.fontSize(26)
.fontWeight(500)
}
.width('100%')
.height('100%')
}
}
Updating Page Content
You can update page content by updating component status. Below is a simple example.
NOTE
Before updating the status of a component, initialize its member variables. The member variables of a custom component can be initialized in either of the following methods: local initialization or initialization using constructor parameters. Select the appropriate method based on the decorator used by the variable.
Example
// xxx.ets
@Entry
@Component
struct ParentComp {
@State isCountDown: boolean = true
build() {
Column() {
Text(this.isCountDown ? 'Count Down' : 'Stopwatch').fontSize(20).margin(20)
if (this.isCountDown) {
// The image resources are stored in the media directory.
Image($r("app.media.countdown")).width(120).height(120)
TimerComponent({ counter: 10, changePerSec: -1, showInColor: Color.Red })
} else {
// The image resources are stored in the media directory.
Image($r("app.media.stopwatch")).width(120).height(120)
TimerComponent({ counter: 0, changePerSec: +1, showInColor: Color.Black })
}
Button(this.isCountDown ? 'Switch to Stopwatch' : 'Switch to Count Down')
.onClick(() => {
this.isCountDown = !this.isCountDown
})
}.width('100%')
}
}
// Custom timer/countdown component.
@Component
struct TimerComponent {
@State counter: number = 0
private changePerSec: number = -1
private showInColor: Color = Color.Black
private timerId: number = -1
build() {
Text(`${this.counter}sec`)
.fontColor(this.showInColor)
.fontSize(20)
.margin(20)
}
aboutToAppear() {
this.timerId = setInterval(() => {
this.counter += this.changePerSec
}, 1000)
}
aboutToDisappear() {
if (this.timerId > 0) {
clearTimeout(this.timerId)
this.timerId = -1
}
}
}
Initial creation and rendering:
Create the parent component ParentComp.
Locally initialize the state variable isCountDown of ParentComp.
Execute the build function of ParentComp.
Create a <Column> component.
- Create a <Text> component, set the text content, and add the <Text> component instance to the <Column> component.
- With the if statement, create a component under the true condition.
- Create an <Image> component and set the image source address.
- Create a TimerComponent using the given constructor.
- Create a <Button> component and set the corresponding content.
Status update:
When the user clicks a button:
The value of the isCountDown state variable of ParentComp is changed to false.
The build function of ParentComp is executed.
The <Column> component is reused and reinitialized.
The child components of <Column> reuse and reinitialize the objects in the memory.
- The <Text> component is reused after being re-initialized using the new text content.
- With the if statement, the components under the false condition are used.
- The component instances created under the true condition are destroyed.
- Components under the false condition are created.
- The <Button> component is reused with the new image source.
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Multi-Language Capability
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦