在 OpenHarmony(ArkUI) 里,“高级 UI 组件”通常指 ArkUI 提供的能力较强、常用于复杂交互或业务场景的组件,比如:
下面用 ArkTS(Stage 模型) 给你一个清晰、实战向的使用说明。
EntryAbility.ts + pages/xxx.ets@Entry
@Component
struct Index {
build() {
Column() {
Text('高级 UI 示例')
}
}
}
@Entry
@Component
struct ListDemo {
private data: string[] = ['A', 'B', 'C', 'D']
build() {
List() {
ForEach(this.data, (item: string) => {
ListItem() {
Text(item)
.fontSize(20)
.padding(10)
}
})
}
.width('100%')
.height('100%')
}
}
✅ 常用于:
@Entry
@Component
struct TabsDemo {
build() {
Tabs() {
TabContent() {
Text('首页')
}.tabBar('首页')
TabContent() {
Text('我的')
}.tabBar('我的')
}
}
}
✅ 适合:
@Entry
@Component
struct NavDemo {
build() {
Navigation() {
Text('主页内容')
}
.title('主页')
}
}
配合:
this.pageStack.pushPath({ name: 'Detail' })
✅ 用于:
@Entry
@Component
struct SwiperDemo {
build() {
Swiper() {
Text('1').fontSize(30)
Text('2').fontSize(30)
Text('3').fontSize(30)
}
.height(200)
.loop(true)
}
}
✅ 常用于:
@Entry
@Component
struct RefreshDemo {
@State isRefreshing: boolean = false
build() {
Refresh({ refreshing: this.isRefreshing }) {
List() {
ListItem() {
Text('内容')
}
}
}
.onRefreshing(() => {
setTimeout(() => {
this.isRefreshing = false
}, 1000)
})
}
}
@Entry
@Component
struct SheetDemo {
@State show: boolean = false
build() {
Column() {
Button('打开')
.onClick(() => this.show = true)
}
.bindSheet(this.show, () => {
Column() {
Text('底部弹窗')
}.height(300)
}, {
onDisappear: () => this.show = false
})
}
}
✅ 替代旧 Dialog 的推荐方式
@Entry
@Component
struct CanvasDemo {
build() {
Canvas(this.context)
.width('100%')
.height(300)
.onReady(() => {
this.context.fillStyle = '#ff0000'
this.context.fillRect(0, 0, 100, 100)
})
}
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D()
}
✅ 用于:
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct WebDemo {
controller: web_webview.WebviewController = new web_webview.WebviewController()
build() {
Web({ src: 'https://www.example.com', controller: this.controller })
.width('100%')
.height('100%')
}
}
@State scale: number = 1
Button('放大')
.scale({ x: this.scale, y: this.scale })
.onClick(() => {
animateTo({ duration: 300 }, () => {
this.scale = 1.5
})
})
✅ 顺序:
你现在是 刚入门 还是 已经在做项目了?
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。