
https://www.bilibili.com/video/BV1jomdBBE4H/

IconButton 是控件库中的图标按钮组件,仅显示图标,无文字,适用于工具栏、操作按钮等场景。与文字按钮的主要区别在于:
图标按钮采用极简设计,具有以下特点:
为了确保图标在所有系统上都能正确显示,推荐使用以下 ASCII 字符:
字符 | 含义 | 用途 |
|---|---|---|
S | Settings(设置) | 设置、配置相关操作 |
U | User(用户) | 用户、账户相关操作 |
R | Refresh(刷新) | 刷新、重新加载 |
D | Delete(删除) | 删除操作 |
E | Edit(编辑) | 编辑、修改操作 |
+ | Add(添加) | 添加、新建操作 |
√ | Check(确认) | 确认、成功操作 |
× | Close(关闭) | 关闭、取消操作 |
... | More(更多) | 更多选项 |
B | Bold(粗体) | 文本格式化 |
I | Italic(斜体) | 文本格式化 |
< = > | 对齐 | 左对齐、居中、右对齐 |
import { IconButton } from '../components/base'
@Entry
@Component
struct MyPage {
build() {
Column({ space: 20 }) {
// 基础圆形图标按钮(使用文字图标)
IconButton({
textIcon: 'S' // S = Settings(设置)
})
// 方形图标按钮
IconButton({
textIcon: '...', // ... = More(更多)
shape: 'square'
})
// 不同尺寸
Row({ space: 12 }) {
IconButton({
textIcon: '+',
buttonSize: 'small'
})
IconButton({
textIcon: '+',
buttonSize: 'medium'
})
IconButton({
textIcon: '+',
buttonSize: 'large'
})
}
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}IconButton 的 onClickBuilder 属性是 @BuilderParam 类型,需要使用 @Builder 或 @LocalBuilder 方法初始化。在实际项目中,推荐使用以下方式处理点击事件:
@State 变量和普通 Button 的 onClick 来处理onClickBuilder,需要定义 @Builder 方法本文档中的示例主要展示按钮的视觉效果和样式配置,点击事件处理可以通过上述方式实现。
属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
icon | ResourceStr? | undefined | 按钮图标(可选,图片资源) |
textIcon | string? | undefined | 文字图标(可选,ASCII字符或Unicode字符,优先级高于 icon。推荐使用:S=设置, U=用户, R=刷新, D=删除, E=编辑, +=添加, √=确认, ×=关闭) |
shape | 'circle' | 'square' | 'circle' | 按钮形状 |
buttonSize | 'small' | 'medium' | 'large' | 'medium' | 按钮尺寸 |
loading | boolean | false | 是否加载中 |
disabled | boolean | false | 是否禁用 |
showBrand | boolean | true | 是否显示品牌标识 |
iconColor | string? | undefined | 图标颜色(可选,默认使用主题色) |
buttonBackgroundColor | string? | undefined | 背景颜色(可选,默认透明) |
onClickBuilder | @BuilderParam () => void? | undefined | 点击事件回调(需要使用 @Builder 或 @LocalBuilder 方法) |
尺寸 | 按钮大小 | 图标大小 | 圆角(圆形) | 圆角(方形) |
|---|---|---|---|---|
small | 32vp × 32vp | 16vp | 16vp | 4vp |
medium | 40vp × 40vp | 20vp | 20vp | 8vp |
large | 80vp × 80vp | 40vp | 40vp | 12vp |
@Entry
@Component
struct ButtonExample1 {
build() {
Column({ space: 15 }) {
// 圆形图标按钮(使用文字图标)
IconButton({
textIcon: 'S' // S = Settings(设置)
})
// 方形图标按钮
IconButton({
textIcon: '...', // ... = More(更多)
shape: 'square'
})
// 带背景的图标按钮
IconButton({
textIcon: '+', // + = Add(添加)
buttonBackgroundColor: '#F0F7FF'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonExample2 {
build() {
Column({ space: 15 }) {
Row({ space: 12 }) {
IconButton({
textIcon: 'S', // S = Settings(设置)
buttonSize: 'small'
})
IconButton({
textIcon: 'S',
buttonSize: 'medium'
})
IconButton({
textIcon: 'S',
buttonSize: 'large'
})
}
.width('100%')
.justifyContent(FlexAlign.Center)
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonExample3 {
build() {
Column({ space: 15 }) {
Row({ space: 12 }) {
// 圆形按钮
IconButton({
textIcon: 'U', // U = User(用户)
shape: 'circle'
})
// 方形按钮
IconButton({
textIcon: 'U',
shape: 'square'
})
}
.width('100%')
.justifyContent(FlexAlign.Center)
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonExample4 {
build() {
Column({ space: 15 }) {
Row({ space: 12 }) {
// 默认颜色
IconButton({
textIcon: 'S' // S = Settings(设置)
})
// 自定义图标颜色
IconButton({
textIcon: '√', // √ = Check(确认/成功)
iconColor: '#34C759'
})
// 自定义背景颜色
IconButton({
textIcon: 'S',
buttonBackgroundColor: '#F0F7FF'
})
// 自定义图标和背景颜色
IconButton({
textIcon: '×', // × = Close(关闭/删除)
iconColor: '#FF3B30',
buttonBackgroundColor: '#FFEBEE'
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceAround)
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonExample5 {
@State loading: boolean = false
// 处理加载操作
handleLoad() {
this.loading = true
// 模拟异步操作
setTimeout(() => {
this.loading = false
console.info('加载完成')
}, 2000)
}
build() {
Column({ space: 20 }) {
IconButton({
textIcon: 'R', // R = Refresh(刷新)
loading: this.loading
})
// 使用普通按钮触发加载状态
Button() {
Text(this.loading ? '加载中...' : '点击刷新')
}
.onClick(() => {
this.handleLoad()
})
.enabled(!this.loading)
Text('点击按钮查看加载状态')
.fontSize(12)
.fontColor('#999999')
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonExample6 {
@State canDelete: boolean = false
build() {
Column({ space: 20 }) {
IconButton({
textIcon: 'D', // D = Delete(删除)
disabled: !this.canDelete
})
Row({ space: 10 }) {
Text('删除权限:')
.fontSize(14)
Toggle({ type: ToggleType.Switch, isOn: this.canDelete })
.onChange((isOn: boolean) => {
this.canDelete = isOn
})
Text(this.canDelete ? '已开启' : '已关闭')
.fontSize(14)
.fontColor(this.canDelete ? '#10B981' : '#EF4444')
}
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ToolbarExample {
build() {
Column({ space: 20 }) {
// 工具栏
Row({ space: 8 }) {
IconButton({
textIcon: 'B',
buttonSize: 'small',
shape: 'square'
})
IconButton({
textIcon: 'I',
buttonSize: 'small',
shape: 'square'
})
IconButton({
textIcon: 'U',
buttonSize: 'small',
shape: 'square'
})
Divider()
.vertical(true)
.height(24)
.margin({ left: 8, right: 8 })
IconButton({
textIcon: '<', // < = 左对齐
buttonSize: 'small',
shape: 'square'
})
IconButton({
textIcon: '=', // = = 居中对齐
buttonSize: 'small',
shape: 'square'
})
IconButton({
textIcon: '>', // > = 右对齐
buttonSize: 'small',
shape: 'square'
})
}
.width('100%')
.padding(12)
.backgroundColor('#F9FAFB')
.borderRadius(8)
}
.width('100%')
.height('100%')
.padding(20)
}
}@Entry
@Component
struct ListActionExample {
build() {
Column({ space: 20 }) {
// 列表项操作
Row({ space: 12 }) {
Text('文件名称.txt')
.fontSize(14)
.layoutWeight(1)
IconButton({
textIcon: 'E', // E = Edit(编辑)
buttonSize: 'small',
shape: 'square'
})
IconButton({
textIcon: 'D', // D = Delete(删除)
buttonSize: 'small',
shape: 'square',
iconColor: '#FF3B30'
})
}
.width('100%')
.padding(12)
.backgroundColor('#FFFFFF')
.borderRadius(8)
.shadow({ radius: 4, color: '#00000010', offsetX: 0, offsetY: 2 })
}
.width('100%')
.height('100%')
.padding(20)
}
}@Entry
@Component
struct CardActionExample {
build() {
Column({ space: 20 }) {
// 卡片右上角操作
Stack() {
Column({ space: 8 }) {
Text('卡片标题')
.fontSize(16)
.fontWeight(FontWeight.Bold)
Text('卡片内容描述')
.fontSize(14)
.fontColor('#666666')
}
.width('100%')
.alignItems(HorizontalAlign.Start)
IconButton({
textIcon: '...', // ... = More(更多)
buttonSize: 'small',
shape: 'circle'
})
.align(Alignment.TopEnd)
}
.width('100%')
.padding(16)
.backgroundColor('#FFFFFF')
.borderRadius(12)
.shadow({ radius: 4, color: '#00000010', offsetX: 0, offsetY: 2 })
}
.width('100%')
.height('100%')
.padding(20)
}
}@Entry
@Component
struct ButtonGroupExample {
build() {
Column({ space: 20 }) {
// 操作按钮组
Row({ space: 12 }) {
IconButton({
textIcon: '+',
buttonSize: 'medium',
shape: 'circle',
buttonBackgroundColor: '#F0F7FF'
})
IconButton({
textIcon: 'E', // E = Edit(编辑)
buttonSize: 'medium',
shape: 'circle'
})
IconButton({
textIcon: 'D', // D = Delete(删除)
buttonSize: 'medium',
shape: 'circle',
iconColor: '#FF3B30'
})
}
.width('100%')
.justifyContent(FlexAlign.Center)
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}IconButton 的所有样式都通过 ComponentTheme 配置,所有配置都在代码中,不依赖JSON文件。
import { ComponentTheme } from '../theme/ComponentTheme'
// 修改主题主色(影响 IconButton 的默认图标颜色)
ComponentTheme.PRIMARY_COLOR = '#007AFF'
// 修改文字主色(影响 IconButton 的默认图标颜色)
ComponentTheme.TEXT_PRIMARY = '#000000'
// 修改禁用文字颜色
ComponentTheme.TEXT_DISABLED = '#CCCCCC'
// 修改背景次要色(影响禁用状态的背景)
ComponentTheme.BACKGROUND_SECONDARY = '#F5F5F5'import { ComponentTheme } from '../theme/ComponentTheme'
// 使用 setTheme 方法批量配置
ComponentTheme.setTheme({
primaryColor: '#007AFF',
borderRadius: 8,
spacing: 16
})// 在应用启动时配置
import { ComponentTheme } from './theme/ComponentTheme'
// 设置自定义主色
ComponentTheme.TEXT_PRIMARY = '#34C759' // 绿色推荐:根据使用场景选择形状
A: 主要区别在于显示内容:
IconButton 更适合工具栏、操作按钮等需要节省空间的场景。
A: 根据使用场景选择:
A: 有两种方式:
IconButton({
textIcon: 'S', // S = Settings(设置)
iconColor: '#34C759',
buttonBackgroundColor: '#F0F9F4'
})ComponentTheme.TEXT_PRIMARY = '#34C759'
ComponentTheme.BACKGROUND_SECONDARY = '#F0F9F4'A: onClickBuilder 是 @BuilderParam 类型,需要使用 @Builder 或 @LocalBuilder 方法。推荐使用状态管理或自定义事件系统:
@Entry
@Component
struct MyPage {
@State count: number = 0
handleClick() {
this.count++
console.info('按钮被点击')
}
build() {
Column({ space: 20 }) {
// 使用普通按钮处理点击
Button() {
Image($r('app.media.icon_settings'))
.width(20)
.height(20)
}
.onClick(() => {
this.handleClick()
})
// 使用 IconButton 展示样式
IconButton({
textIcon: 'S' // S = Settings(设置)
})
Text(`点击次数:${this.count}`)
}
.width('100%')
.height('100%')
.padding(20)
}
}A: 可以。IconButton 非常适合用于工具栏场景:
Row({ space: 8 }) {
IconButton({
textIcon: 'B', // B = Bold(粗体)
buttonSize: 'small',
shape: 'square'
})
IconButton({
textIcon: 'I', // I = Italic(斜体)
buttonSize: 'small',
shape: 'square'
})
}A: 目前支持三种预设尺寸(small、medium、large)。如果需要自定义大小,可以通过修改 ComponentTheme 中的相关配置,或者使用其他按钮组件(如 TextButton)配合图标实现。
IconButton 是控件库中的图标按钮组件,具有以下核心特性:
shape 属性选择合适形状buttonSize 属性选择合适尺寸iconColor 和 buttonBackgroundColor 自定义颜色loading 属性处理异步操作disabled 属性控制按钮状态ComponentTheme 自定义全局样式下一篇预告:ButtonGroup(按钮组)详解
本文档属于《鸿蒙PC UI控件库开发系列文章》第5篇