• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <toolbar> Development
2
3
4The **<toolbar>** component shows actions available on the current screen and can be used for level-1 navigation. For details, see [toolbar](../reference/arkui-js/js-components-basic-toolbar.md).
5
6
7## Creating a <toolbar> Component
8
9Create a **<toolbar>** component in the .hml file under **pages/index**.
10
11
12```html
13<!-- xxx.hml -->
14<div class="container">
15  <toolbar style="background-color: #F1F3F5;">
16    <toolbar-item value="item1"></toolbar-item>
17    <toolbar-item value="item2"></toolbar-item>
18  </toolbar>
19</div>
20```
21
22
23```css
24/* xxx.css */
25.container {
26  width: 100%;
27  height: 100%;
28  flex-direction: column;
29  justify-content: center;
30  align-items: center;
31  background-color: #F1F3F5;
32}
33toolbar-item{
34  font-size: 35px;
35}
36```
37
38![en-us_image_0000001275922977](figures/en-us_image_0000001275922977.gif)
39
40
41## Adding Child Components
42
43The **&lt;toolbar&gt;** component supports only the **&lt;toolbar-item&gt;** child component and can display a maximum of five **&lt;toolbar-item&gt;** child components on a page. If there are six or more **&lt;toolbar-item&gt;** child components, the first four child components are retained, and the rest are moved to the **More** option on the toolbar and can be displayed on a pop-up window by clicking **More**. Under **More**, the child components are displayed in the default style; the custom style settings do not take effect.
44
45```html
46<!-- xxx.hml -->
47<div class="container">
48  <toolbar>
49    <toolbar-item value="item1"></toolbar-item>
50    <toolbar-item value="item2"></toolbar-item>
51    <toolbar-item value="item3"></toolbar-item>
52    <toolbar-item value="item4"></toolbar-item>
53    <toolbar-item value="item5"></toolbar-item>
54    <toolbar-item value="item6"></toolbar-item>
55  </toolbar>
56</div>
57```
58
59
60```css
61/* xxx.css */
62.container {
63  width: 100%;
64  height: 100%;
65  flex-direction: column;
66  justify-content: center;
67  align-items: center;
68  background-color: #F1F3F5;
69}
70toolbar-item{
71  font-size: 35px;
72}
73```
74
75![en-us_image_0000001232002988](figures/en-us_image_0000001232002988.gif)
76
77
78## Setting Styles
79
80Set the **position** style for the **&lt;toolbar&gt;** component and set the font color, size, and background color of **&lt;toolbar-item&gt;** child components.
81
82
83
84```html
85<!-- xxx.hml -->
86<div class="container">
87  <toolbar style="position: fixed;bottom: 5%;width: 100%;background-color: #F1F3F5;">
88    <toolbar-item value="item1" icon="common/images/1.png" class="toolbarActive"></toolbar-item>
89    <toolbar-item value="item2" icon="common/images/2.png"></toolbar-item>
90    <toolbar-item value="item3" icon="common/images/1.png"></toolbar-item>
91    <toolbar-item value="item4" icon="common/images/2.png"></toolbar-item>
92  </toolbar>
93</div>
94```
95
96
97
98```css
99/* xxx.css */
100.container {
101  background-color: #F1F3F5;
102  flex-direction: column;
103  width: 100%;
104  height: 100%;
105  justify-content: center;
106  align-items: center;
107}
108toolbar-item{
109  font-size: 35px;
110}
111```
112
113
114![en-us_image_0000001275803149](figures/en-us_image_0000001275803149.png)
115
116
117## Binding Events
118
119Bind the click event and long press event to the **&lt;toolbar-item&gt;** child components, so that the text of these components turns red upon click and turns blue upon long press.
120
121
122```html
123<!-- xxx.hml -->
124<div class="container">
125  <toolbar style="position: fixed;top: 50%;width: 100%;background-color: #F1F3F5;">
126    <toolbar-item value="item1" icon="common/images/1.png" style="color: {{itemColor}};" onclick="itemClick"></toolbar-item>
127    <toolbar-item value="item2" icon="common/images/2.png"  style="color: {{itemColor}}"></toolbar-item>
128    <toolbar-item value="item3" icon="common/images/3.png"  style="color: {{itemColor}}" onlongpress="itemLongPress"></toolbar-item>
129  </toolbar>
130</div>
131```
132
133
134```css
135/* xxx.css */
136.container {
137  background-color: #F1F3F5;
138  flex-direction: column;
139  width: 100%;
140  height: 100%;
141  justify-content: center;
142  align-items: center;
143}
144toolbar-item{
145  font-size: 35px;
146}
147```
148
149
150```js
151// xxx.js
152import promptAction from '@ohos.promptAction';
153export default {
154  data:{
155    itemColor:'black'
156  },
157  itemClick(){
158    this.itemColor= "red";
159    promptAction.showToast({duration:2000,message:'item click'});
160  },
161  itemLongPress(){
162    promptAction.showToast({duration:2000,message:'item long press'});
163    this.itemColor= "blue";
164  },
165}
166```
167
168![en-us_image_0000001275803153](figures/en-us_image_0000001275803153.gif)
169
170> **NOTE**
171>
172> The **&lt;toolbar&gt;** component does not allow adding of events or methods, but its child components do.
173
174
175## Example Scenario
176
177In this example, you'll implement a **&lt;toolbar-item&gt;** component, clicking which will trigger a change in the text color and the image corresponding to the component.
178
179Use the **for** loop to create a **&lt;toolbar-item&gt;** component and bind a click event to it, so that clicking the component will obtain and store an index value. When setting the text color, the system checks whether the current index value is the stored value. If yes, the system sets the color to red. If no, the system uses the default color.
180
181```html
182<!-- xxx.hml -->
183<div class="container">
184  <image src="{{imgList[active]}}"></image>
185  <toolbar style="position: fixed;bottom: 5%;width: 100%;background-color: #F1F3F5;">
186    <toolbar-item value="{{ item.option}}" icon="{{item.icon}}" style="color: {{active == $idx?'red':'black'}};background-color: {{active== $idx?'#dbe7f1':'#F1F3F5'}};" for="{{item in itemList}}" onclick="itemClick({{$idx}})"></toolbar-item>
187    </toolbar>
188</div>
189```
190
191
192```css
193/* xxx.css */
194.container {
195  background-color: #F1F3F5;
196  flex-direction: column;
197  width: 100%;
198  justify-content: center;
199  align-items: center;
200}
201toolbar-item{
202  font-size: 35px;
203}
204```
205
206
207```js
208// xxx.js
209export default {
210  data:{
211    active: 0,
212    imgList:["common/images/1.png","common/images/2.png","common/images/3.png","common/images/4.png"],
213    itemList:[
214      {option:'item1',icon:'common/images/1.png'},
215      {option:'item2',icon:'common/images/2.png'},
216      {option:'item3',icon:'common/images/3.png'},
217      {option:'item4',icon:'common/images/4.png'},
218    ]
219  },
220  itemClick(id){
221    this.active= id;
222  },
223}
224```
225
226![en-us_image_0000001231683128](figures/en-us_image_0000001231683128.gif)
227