用 ArkUI 组件打造独特界面的实操方案
一 设计思路与组件选型
二 界面骨架与布局策略
三 视觉差异化与交互反馈
四 复用与工程化实践
五 快速上手示例
// 1) 主题与资源:在 resources/base/element/color.json 定义
// { "color": { "primary": "#3A7BD5", "onPrimary": "#FFFFFF", "surface": "#FFFFFF", "onSurface": "#000000" } }
// 2) 可复用头部组件
@Component
struct AppHeader {
build() {
Row() {
Image($r('app.media.ic_logo')).width(32).height(32)
Text('MyApp').fontSize(20).fontWeight(FontWeight.Bold).margin({ left: 8 })
}
.width('100%')
.height(56)
.justifyContent(FlexAlign.Start)
.padding({ left: 16 })
.backgroundColor($r('app.color.primary'))
.shadow({ radius: 4, color: '#1A000000', offsetY: 2 })
}
}
// 3) 多态样式按钮
@Styles function pressed() { .backgroundColor('#2C5AA0') }
@Styles function normal() { .backgroundColor($r('app.color.primary')) }
// 4) 首页
@Entry
@Component
struct HomePage {
@State currentIndex: number = 0
@State darkMode: boolean = false
private swiperController: SwiperController = new SwiperController()
build() {
Column() {
// 头部
AppHeader()
// 主题切换
Row() {
Text('深色模式').fontSize(14)
Toggle({ type: ToggleType.Switch, isOn: this.darkMode })
.onChange((isOn: boolean) => { this.darkMode = isOn })
}
.width('90%')
.justifyContent(FlexAlign.SpaceBetween)
.margin({ top: 8, bottom: 8 })
// 轮播
Swiper(this.swiperController) {
Image($r('app.media.banner1')).width('100%').height(180).borderRadius(8)
Image($r('app.media.banner2')).width('100%').height(180).borderRadius(8)
}
.index(this.currentIndex)
.autoPlay(true).interval(4000)
.loop(true)
.indicator(true)
.width('95%')
.height(180)
.margin({ bottom: 12 })
// 功能卡片
Row({ space: 12 }) {
this.FuncCard('通知', $r('app.media.ic_notice'), () => router.pushUrl({ url: 'pages/NoticePage' }))
this.FuncCard('设置', $r('app.media.ic_setting'), () => router.pushUrl({ url: 'pages/SettingPage' }))
}
.width('95%')
.justifyContent(FlexAlign.SpaceBetween)
// 底部导航
Row() {
this.TabItem('首页', 0)
this.TabItem('发现', 1)
this.TabItem('我的', 2)
}
.width('100%')
.height(56)
.backgroundColor($r('app.color.surface'))
.justifyContent(FlexAlign.SpaceAround)
.border({ width: { top: 1 }, color: '#E5E5E5' })
}
.width('100%')
.height('100%')
.backgroundColor(this.darkMode ? '#121212' : $r('app.color.surface'))
}
@Builder FuncCard(label: string, icon: Resource, onClick: () => void) {
Column({ space: 6 }) {
Image(icon).width(40).height(40)
Text(label).fontSize(12).fontColor(this.darkMode ? '#CCCCCC' : '#333333')
}
.width('30%')
.height(100)
.backgroundColor(this.darkMode ? '#1E1E1E' : '#FFFFFF')
.borderRadius(12)
.shadow({ radius: 6, color: '#1A00000010', offsetY: 2 })
.onClick(onClick)
}
@Builder TabItem(label: string, index: number) {
Column({ space: 4 }) {
Text(label).fontSize(12)
}
.onClick(() => { this.currentIndex = index })
.stateStyles({
pressed: { .scale(0.95) },
normal: { .scale(1.0) }
})
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。