首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ArkUI 组件开发案例参考

ArkUI 组件开发案例参考

作者头像
程序猿的栖息地
发布2026-07-07 15:28:31
发布2026-07-07 15:28:31
20
举报

ArkUI 组件开发参考

基础组件

Text 文本组件

代码语言:javascript
复制
Text('Hello HarmonyOS')
 .fontSize(20)
 .fontColor('#333333')
 .fontWeight(FontWeight.Bold)
 .textAlign(TextAlign.Center)
 .maxLines(2)
 .textOverflow({ overflow: TextOverflow.Ellipsis })
 .decoration({ type: TextDecorationType.Underline, color: '#0066FF' })

Image 图片组件

代码语言:javascript
复制
Image($r('app.media.icon'))
 .width(100)
 .height(100)
 .objectFit(ImageFit.Contain)
 .interpolation(ImageInterpolation.High)
 .alt($r('app.media.placeholder')) // 加载失败占位图
 .onComplete(() => { console.info('Image loaded') })
 .onError(() => { console.error('Image load failed') })

Button 按钮组件

代码语言:javascript
复制
Button('确认提交', { type: ButtonType.Capsule })
 .width(200)
 .height(40)
 .fontSize(16)
 .fontColor(Color.White)
 .backgroundColor('#007DFF')
 .borderRadius(20)
 .stateStyles({
 normal: { .backgroundColor('#007DFF') },
 pressed: { .backgroundColor('#0055CC') },
 disabled: { .backgroundColor('#CCCCCC') }
 })
 .onClick(() => { this.handleSubmit() })

TextInput 输入组件

代码语言:javascript
复制
TextInput({ placeholder: '请输入用户名' })
 .type(InputType.Normal)
 .maxLength(20)
 .caretColor('#007DFF')
 .placeholderColor('#999999')
 .onChange((value) => { this.username = value })
 .onSubmit((enterKey) => { this.doLogin() })

Column / Row 布局组件

代码语言:javascript
复制
// 垂直布局
Column({ space: 10 }) {
 Text('标题')
 Text('内容')
}
.width('100%')
.height(200)
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Start)
.padding({ left: 16, right: 16, top: 10, bottom: 10 })
 
// 水平布局
Row({ space: 8 }) {
 Image($r('app.media.avatar')).width(40).height(40)
 Text('用户名')
}
.width('100%')
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.SpaceBetween)

List 列表组件

代码语言:javascript
复制
List({ space: 12, initialIndex: 0 }) {
 ForEach(this.dataList, (item: DataItem) => {
 ListItem() {
 this.ListItemBuilder(item)
 }
 }, (item: DataItem) => item.id.toString())
}
.width('100%')
.height('100%')
.edgeEffect(EdgeEffect.Spring)
.onReachEnd(() => { this.loadMore() })
.onScroll(() => { this.onScrollEvent() })
 
@Builder
ListItemBuilder(item: DataItem) {
 Row() {
 Image(item.icon).width(48).height(48)
 Column() {
 Text(item.title).fontSize(16).fontWeight(FontWeight.Bold)
 Text(item.subtitle).fontSize(12).fontColor('#999999')
 }.layoutWeight(1)
 Text(item.price + '元').fontSize(14).fontColor('#FF4400')
 }.width('100%').padding(12)
}

Scroll 滚动容器

代码语言:javascript
复制
Scroll() {
 Column() {
 // 大量内容...
 }
}
.width('100%')
.height('100%')
.scrollable(ScrollDirection.Vertical)
.scrollBar(BarState.Auto)
.scrollBarColor('#CCCCCC')
.edgeEffect(EdgeEffect.Spring)
.onScrollFrameBegin((offset) => {
 return { offsetRemain: offset }
})

Tabs 标签页

代码语言:javascript
复制
Tabs({ barPosition: BarPosition.End }) {
 TabContent() {
 this.HomeContent()
 }.tabBar('首页')
 
 TabContent() {
 this.CategoryContent()
 }.tabBar('分类')
 
 TabContent() {
 this.ProfileContent()
 }.tabBar('我的')
}
.vertical(false)
.scrollable(true)
.barMode(BarMode.Fixed)
.barWidth(360)
.barHeight(56)
.onChange((index) => { this.currentTab = index })

Grid 网格布局

代码语言:javascript
复制
Grid() {
 ForEach(this.gridData, (item: GridItem) => {
 GridItem() {
 Column() {
 Image(item.icon).width(48).height(48)
 Text(item.name).fontSize(12).margin({ top: 4 })
 }
 }
 }, (item: GridItem) => item.id.toString())
}
.columnsTemplate('1fr 1fr 1fr 1fr')
.rowsGap(10)
.columnsGap(10)
.width('100%')
.height(200)
.padding({ left: 12, right: 12 })

Swiper 轮播组件

代码语言:javascript
复制
Swiper() {
 ForEach(this.bannerList, (item: BannerItem) => {
 Image(item.imageUrl)
 .width('100%')
 .height(180)
 .objectFit(ImageFit.Cover)
 }, (item: BannerItem) => item.id.toString())
}
.autoPlay(true)
.interval(3000)
.indicator(true)
.loop(true)
.onChange((index) => { this.currentBanner = index })

高级组件

CustomDialog 自定义弹窗

代码语言:javascript
复制
@CustomDialog
struct MyCustomDialog {
 controller: CustomDialogController
 title: string = '提示'
 message: string = ''
 onConfirm: () => void = () => {}
 
 build() {
 Column({ space: 16 }) {
 Text(this.title).fontSize(18).fontWeight(FontWeight.Bold)
 Text(this.message).fontSize(14).fontColor('#666666')
 Row({ space: 12 }) {
 Button('取消')
 .onClick(() => { this.controller.close() })
 Button('确认')
 .onClick(() => { this.onConfirm(); this.controller.close() })
 }
 }.padding(24).width('80%')
 }
}
 
// 使用:
dialogController: CustomDialogController = new CustomDialogController({
 builder: MyCustomDialog({
 title: '确认删除',
 message: '此操作不可撤销',
 onConfirm: () => { this.doDelete() }
 }),
 autoCancel: true,
 alignment: DialogAlignment.Center
})

Panel 面板

代码语言:javascript
复制
Panel(this.showPanel) {
 Column() {
 Text('面板标题').fontSize(18).padding(16)
 // 面板内容
 }
}
.type(PanelTypeType.MINI)
.mode(PanelMode.HALF)
.dragBar(true)
.showClose(true)
.onHeightChange((height: number) => { console.info(`Panel height: ${height}`) })

Refresh 下拉刷新

代码语言:javascript
复制
Refresh({ refreshing: $this.isRefreshing }) {
 List() {
 ForEach(this.dataList, (item) => {
 ListItem() { this.ListItemBuilder(item) }
 })
 }
}
.onRefreshing(() => {
 this.refreshData()
})
.refreshParams({
 refreshingColor: '#007DFF',
 maxDistance: 80
})

LoadingProgress 加载动画

代码语言:javascript
复制
LoadingProgress()
 .color('#007DFF')
 .width(48)
 .height(48)

Stepper 步骤导航

代码语言:javascript
复制
Stepper({ index: $this.currentStep }) {
 StepperItem() {
 Column() { Text('步骤1内容') }
 }.nextLabel('下一步').prevLabel('返回')
 
 StepperItem() {
 Column() { Text('步骤2内容') }
 }.nextLabel('下一步')
 
 StepperItem() {
 Column() { Text('步骤3内容') }
 }.nextLabel('完成')
}
.onFinish(() => { this.handleSubmit() })
.onNext((index) => { this.validateStep(index) })
.onBack((index) => { this.onStepBack(index) })

Blank 空白占位

代码语言:javascript
复制
Row() {
 Text('左侧')
 Blank().layoutWeight(1) // 自动填充剩余空间
 Text('右侧')
}.width('100%')

通用属性速查

属性

说明

示例

.width() / .height()

宽高

.width(200).height(40)

.padding()

内边距

.padding({ left: 16, right: 16, top: 8, bottom: 8 })

.margin()

外边距

.margin(10) 或 .margin({ top: 10 })

.borderRadius()

圆角

.borderRadius(8)

.border()

边框

.border({ width: 1, color: '#CCCCCC', style: BorderStyle.Solid })

.backgroundColor()

背景色

.backgroundColor('#FFFFFF')

.opacity()

透明度

.opacity(0.5)

.enabled()

是否可用

.enabled(false)

.visibility()

可见性

.visibility(this.isLoading ? Visibility.None : Visibility.Visible)

.flexGrow() / .layoutWeight()

弹性布局

.layoutWeight(1)

.position()

绝对定位

.position({ x: 10, y: 20 })

.zIndex()

层级

.zIndex(10)

.shadow()

阴影

.shadow({ radius: 10, color: '#33000000', offsetX: 2, offsetY: 4 })

.linearGradient()

线性渐变

.linearGradient({ direction: GradientDirection.Right, colors: [['#007DFF', 0], ['#00BBFF', 1]] })

.animation()

动画

.animation({ duration: 300, curve: Curve.EaseInOut })

.gesture()

手势

.gesture(TapGesture().onAction(() => {}))

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2026-07-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿的栖息地 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ArkUI 组件开发参考
  • 基础组件
    • Text 文本组件
    • Image 图片组件
    • Button 按钮组件
    • TextInput 输入组件
    • Column / Row 布局组件
    • List 列表组件
    • Scroll 滚动容器
    • Tabs 标签页
    • Grid 网格布局
    • Swiper 轮播组件
  • 高级组件
    • CustomDialog 自定义弹窗
    • Panel 面板
    • Refresh 下拉刷新
    • LoadingProgress 加载动画
    • Stepper 步骤导航
    • Blank 空白占位
  • 通用属性速查
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档