harmony 鸿蒙ToolBar
ToolBar
The Toolbar component is designed to present a set of action options related to the current screen, displayed at the bottom of the screen. It can display up to five child components. If there are six or more child components, the first four are shown directly, and the additional ones are grouped under a More item on the rightmost side of the toolbar.
NOTE
This component is supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.
Modules to Import
import { SymbolGlyphModifier, DividerModifier, ToolBar, ToolBarOptions, ToolBarModifier, ItemState, LengthMetrics } from '@kit.ArkUI';
Child Components
Not supported
Attributes
The universal attributes are not supported.
ToolBar
Toolbar({toolBarList: ToolBarOptions, activateIndex?: number, controller: TabsController, dividerModifier?: DividerModifier, toolBarModifier?: ToolBarModifier})
Decorator: @Component
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Decorator | Description |
---|---|---|---|---|
toolBarList | ToolBarOptions | Yes | @ObjectLink | Toolbar list. Atomic service API: This API can be used in atomic services since API version 11. |
activateIndex | number | No | @Prop | Index of the active item. The value must be greater than or equal to -1. The default value is -1, indicating that there is no active item. Values less than -1 are treated as no active item. Atomic service API: This API can be used in atomic services since API version 11. |
controller | TabsController | Yes | - | Toolbar controller, which cannot be used for controlling individual toolbar items. Atomic service API: This API can be used in atomic services since API version 11. |
dividerModifier13+ | DividerModifier | No | @Prop | Modifier for the toolbar header divider, which can be used to customize the divider’s height, color, and other attributes. Default value: system default value Atomic service API: This API can be used in atomic services since API version 13. |
toolBarModifier13+ | ToolBarModifier | No | @Prop | Modifier for the toolbar, which can be used to set the toolbar’s height, background color, padding (which only takes effect when there are fewer than five toolbar items), and whether to display the pressed state. Default value: Height of the toolbar: 56vp Background color: ohos_id_toolbar_bg Padding: 24vp Whether to display the pressed state: yes Atomic service API: This API can be used in atomic services since API version 13. |
ToolBarOptions
Inherits Array<ToolBarOption>.
Decorator Type: \@Observed
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
ToolBarOption
Decorator Type: \@Observed
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
content | ResourceStr | Yes | Text of the toolbar item. Atomic service API: This API can be used in atomic services since API version 11. |
action | () => void | No | Click event of the toolbar item. Atomic service API: This API can be used in atomic services since API version 11. |
icon | Resource | No | Icon of the toolbar item. If toolBarSymbolOptions has input parameters, icon is ineffective. Atomic service API: This API can be used in atomic services since API version 11. |
state | ItemState | No | State of the toolbar item. Default value: ENABLE Atomic service API: This API can be used in atomic services since API version 11. |
iconColor13+ | ResourceColor | No | Icon fill color of the toolbar item. Default value: $r(‘sys.color.icon_primary’) Atomic service API: This API can be used in atomic services since API version 13. |
activatedIconColor13+ | ResourceColor | No | Icon fill color of the toolbar option in the activated state. Default value: $r(‘sys.color.icon_emphasize’) Atomic service API: This API can be used in atomic services since API version 13. |
textColor13+ | ResourceColor | No | Font color of the toolbar item. Default value: $r(‘sys.color.font_primary’) Atomic service API: This API can be used in atomic services since API version 13. |
activatedTextColor13+ | ResourceColor | No | Font color of the toolbar item in the activated state. Default value: $r(‘sys.color.font_emphasize’) Atomic service API: This API can be used in atomic services since API version 13. |
toolBarSymbolOptions13+ | ToolBarSymbolGlyphOptions | No | Icon symbol options of the toolbar item. Atomic service API: This API can be used in atomic services since API version 13. |
accessibilityText18+ | ResourceStr | No | Accessibility text, that is, accessible label name, of the toolbar item. If a component does not contain text information, it will not be announced by the screen reader when selected. In this case, the screen reader user cannot know which component is selected. To solve this problem, you can set accessibility text for components without text information. When such a component is selected, the screen reader announces the specified accessibility text, informing the user which component is selected. Default value: value of content Atomic service API: This API can be used in atomic services since API version 18. |
accessibilityDescription18+ | ResourceStr | No | Accessible description of the toolbar item. You can provide comprehensive text explanations to help users understand the operation they are about to perform and its potential consequences, especially when these cannot be inferred from the component’s attributes and accessibility text alone. If a component contains both text information and the accessible description, the text is announced first and then the accessible description, when the component is selected. Default value: “Double-tap to activate” Atomic service API: This API can be used in atomic services since API version 18. |
accessibilityLevel18+ | string | No | Accessibility level of the toolbar item. It determines whether the component can be recognized by accessibility services. The options are as follows: “auto”: It is treated as “yes” by the system. “yes”: The component can be recognized by accessibility services. “no”: The component cannot be recognized by accessibility services. “no-hide-descendants”: Neither the component nor its child components can be recognized by accessibility services. Default value: “auto” Atomic service API: This API can be used in atomic services since API version 18. |
ToolBarModifier13+
Provides APIs for setting the height (height), background color (backgroundColor), left and right padding (padding, which only takes effect when there are fewer than five items) of the toolbar, and whether to display the pressed state effect (stateEffect).
Atomic service API: This API can be used in atomic services since API version 13.
backgroundColor
backgroundColor(backgroundColor: ResourceColor): ToolBarModifier
Sets the background color of the toolbar. By overriding this API, you can implement custom drawing for the background color of the toolbar.
Atomic service API: This API can be used in atomic services since API version 13.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
backgroundColor | ResourceColor | Yes | Toolbar background color Default value: $r(‘sys.color.ohos_id_color_toolbar_bg’) |
padding
padding(padding: LengthMetrics): ToolBarModifier
Sets the left and right padding of the toolbar. By overriding this API, you can implement custom drawing for the left and right padding of the toolbar.
Atomic service API: This API can be used in atomic services since API version 13.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
padding | LengthMetrics12+ | Yes | Left and right padding of the toolbar, which is effective only when there are fewer than five items. By default, the padding is 24 vp when there are fewer than five items and 0 when there are five or more items. |
height
height(height: LengthMetrics): ToolBarModifier
Sets the height of the toolbar. By overriding this API, you can implement custom drawing for the height of the toolbar, which does not include the height of the divider.
Atomic service API: This API can be used in atomic services since API version 13.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
height | LengthMetrics12+ | Yes | Height of the toolbar. The default height of the toolbar is 56 vp, which does not include the divider. |
stateEffect
stateEffect(stateEffect: boolean): ToolBarModifier
Sets whether to display the pressed state effect.
Atomic service API: This API can be used in atomic services since API version 13.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
stateEffect | boolean | Yes | Whether to display the pressed state effect on the toolbar. The value true means to display the pressed state effect on the toolbar, and false means the opposite. Default value: true |
ItemState
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Value | Description |
---|---|---|
ENABLE | 1 | The toolbar item is enabled. |
DISABLE | 2 | The toolbar item is disabled. |
ACTIVATE | 3 | The toolbar item is activated. |
ToolBarSymbolGlyphOptions13+
Defines the icon symbol options.
Atomic service API: This API can be used in atomic services since API version 13.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
normal | SymbolGlyphModifier | No | Icon symbol of the toolbar item in normal state. Default value: fontColor: $r(‘sys.color.icon_primary’), fontSize: 24vp |
activated | SymbolGlyphModifier | No | Icon symbol of the toolbar item in activated state. Default value: fontColor: $r(‘sys.color.icon_emphasize’), fontSize: 24vp |
Events
The universal events are not supported.
Example
Example 1: Setting Toolbar Items to Different States
This example shows the various display effects when the state property of toolbar items is set to ENABLE, DISABLE, or ACTIVATE.
import { ToolBar, ToolBarOptions, ItemState } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State toolbarList: ToolBarOptions = new ToolBarOptions();
aboutToAppear() {
this.toolbarList.push({
content: 'Cut Super Long Text',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
this.toolbarList.push({
content: 'Copy',
icon: $r('sys.media.ohos_ic_public_copy'),
action: () => {
},
state: ItemState.DISABLE
})
this.toolbarList.push({
content: 'Paste',
icon: $r('sys.media.ohos_ic_public_paste'),
action: () => {
},
state: ItemState.ACTIVATE
})
this.toolbarList.push({
content: 'Select All',
icon: $r('sys.media.ohos_ic_public_select_all'),
action: () => {
},
})
this.toolbarList.push({
content: 'Share',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
this.toolbarList.push({
content: 'Share',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
}
build() {
Row() {
Stack() {
Column() {
ToolBar({
activateIndex: 2,
toolBarList: this.toolbarList,
})
}
}
.align(Alignment.Bottom)
.width('100%')
.height('100%')
}
}
}
Example 2: Customizing the Toolbar Style
This example demonstrates how to customize the toolbar’s height, background color, and other styles using ToolBarModifier.
import {
SymbolGlyphModifier,
DividerModifier,
ToolBar,
ToolBarOptions,
ToolBarModifier,
ItemState,
LengthMetrics,
} from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State toolbarList: ToolBarOptions = new ToolBarOptions();
// Custom toolbar style
private toolBarModifier: ToolBarModifier =
new ToolBarModifier().height(LengthMetrics.vp(52)).backgroundColor(Color.Transparent).stateEffect(false);
@State dividerModifier: DividerModifier = new DividerModifier().height(0);
aboutToAppear() {
// Add toolbar items.
this.toolbarList.push({
content: 'Long long long long long long long long text',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
state: ItemState.ACTIVATE,
toolBarSymbolOptions: {
normal: new SymbolGlyphModifier($r('sys.symbol.ohos_star')).fontColor([Color.Green]), // Symbol icon in the normal state.
activated: new SymbolGlyphModifier($r('sys.symbol.ohos_star')).fontColor([Color.Red]), // Symbol icon in the activated state.
},
activatedTextColor: $r('sys.color.font_primary'),
})
this.toolbarList.push({
content: 'Copy',
icon: $r('sys.media.ohos_ic_public_copy'),
action: () => {
},
state: ItemState.DISABLE,
iconColor: '#ff18cb53',
activatedIconColor: '#ffec5d5d', // Icon fill color of the toolbar item in the activated state.
activatedTextColor: '#ffec5d5d', // Font color of the toolbar item in the activated state.
})
this.toolbarList.push({
content: 'Paste',
icon: $r('sys.media.ohos_ic_public_paste'),
action: () => {
},
state: ItemState.ACTIVATE,
textColor: '#ff18cb53',
})
this.toolbarList.push({
content: 'All',
icon: $r('sys.media.ohos_ic_public_select_all'),
action: () => {
},
state: ItemState.ACTIVATE,
})
this.toolbarList.push({
content: 'Share',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
this.toolbarList.push({
content: 'Share',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
}
build() {
Row() {
Stack() {
Column() {
ToolBar({
toolBarModifier: this.toolBarModifier,
dividerModifier: this.dividerModifier,
activateIndex: 0,
toolBarList: this.toolbarList,
})
.height(52)
}
}
.align(Alignment.Bottom)
.width('100%')
.height('100%')
}
}
}
Example 3: Implementing Screen Reader Announcement
This example customizes the screen reader announcement text by setting the accessibilityText, accessibilityDescription, and accessibilityLevel properties of the toolbar item.
import { ToolBar, ToolBarOptions, ItemState } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State toolbarList: ToolBarOptions = new ToolBarOptions();
aboutToAppear() {
// Add toolbar items.
this.toolbarList.push({
content: 'Cut Super Long Text',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
accessibilityText: 'Clip', // Screen reader announcement for the item.
accessibilityDescription: 'Double-tap to clip', // Screen reader announcement for the item.
accessibilityLevel: 'yes' // Configure this element to be focused by accessibility screen readers.
})
this.toolbarList.push({
content: 'Copy',
icon: $r('sys.media.ohos_ic_public_copy'),
action: () => {
},
state: ItemState.DISABLE,
accessibilityLevel: 'no' // Configure this button to be not recognizable by screen readers.
})
this.toolbarList.push({
content: 'Paste',
icon: $r('sys.media.ohos_ic_public_paste'),
action: () => {
},
state: ItemState.ACTIVATE
})
this.toolbarList.push({
content: 'Select All',
icon: $r('sys.media.ohos_ic_public_select_all'),
action: () => {
},
})
this.toolbarList.push({
content: 'Share',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
this.toolbarList.push({
content: 'Share',
icon: $r('sys.media.ohos_ic_public_share'),
action: () => {
},
})
}
build() {
Row() {
Stack() {
Column() {
ToolBar({
activateIndex: 2,
toolBarList: this.toolbarList,
})
}
}
.align(Alignment.Bottom)
.width('100%')
.height('100%')
}
}
}
你可能感兴趣的鸿蒙文章
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦