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 **<toolbar>** component supports only the **<toolbar-item>** child component and can display a maximum of five **<toolbar-item>** child components on a page. If there are six or more **<toolbar-item>** 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 **<toolbar>** component and set the font color, size, and background color of **<toolbar-item>** 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.toolbarActive{ 112 color: red; 113 background-color: #daebef; 114} 115``` 116 117 118![en-us_image_0000001275803149](figures/en-us_image_0000001275803149.png) 119 120 121## Binding Events 122 123Bind the click event and long press event to the **<toolbar-item>** child components, so that the text of these components turns red upon click and turns blue upon long press. 124 125 126```html 127<!-- xxx.hml --> 128<div class="container"> 129 <toolbar style="position: fixed;top: 50%;width: 100%;background-color: #F1F3F5;"> 130 <toolbar-item value="item1" icon="common/images/1.png" style="color: {{itemColor}};" onclick="itemClick"></toolbar-item> 131 <toolbar-item value="item2" icon="common/images/2.png" style="color: {{itemColor}}"></toolbar-item> 132 <toolbar-item value="item3" icon="common/images/3.png" style="color: {{itemColor}}" onlongpress="itemLongPress"></toolbar-item> 133 </toolbar> 134</div> 135``` 136 137 138```css 139/* xxx.css */ 140.container { 141 background-color: #F1F3F5; 142 flex-direction: column; 143 width: 100%; 144 height: 100%; 145 justify-content: center; 146 align-items: center; 147} 148toolbar-item{ 149 font-size: 35px; 150} 151``` 152 153 154```js 155// xxx.js 156import promptAction from '@ohos.promptAction'; 157export default { 158 data:{ 159 itemColor:'black' 160 }, 161 itemClick(){ 162 this.itemColor= "red"; 163 promptAction.showToast({duration:2000,message:'item click'}); 164 }, 165 itemLongPress(){ 166 promptAction.showToast({duration:2000,message:'item long press'}); 167 this.itemColor= "blue"; 168 }, 169} 170``` 171 172![en-us_image_0000001275803153](figures/en-us_image_0000001275803153.gif) 173 174> **NOTE** 175> 176> The **<toolbar>** component does not allow adding of events or methods, but its child components do. 177 178 179## Example Scenario 180 181In this example, you'll implement a **<toolbar-item>** component, clicking which will trigger a change in the text color and the image corresponding to the component. 182 183Use the **for** loop to create a **<toolbar-item>** 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. 184 185```html 186<!-- xxx.hml --> 187<div class="container"> 188 <image src="{{imgList[active]}}"></image> 189 <toolbar style="position: fixed;bottom: 5%;width: 100%;background-color: #F1F3F5;"> 190 <toolbar-item value="{{ item.option}}" icon="{{item.icon}}" style="color: {{active == $idx?'red':'black'}};background-color: {{active 191 == $idx?'#dbe7f1':'#F1F3F5'}};" for="{{item in itemList}}" onclick="itemClick({{$idx}})"></toolbar-item> 192 </toolbar> 193</div> 194``` 195 196 197```css 198/* xxx.css */ 199.container { 200 background-color: #F1F3F5; 201 flex-direction: column; 202 width: 100%; 203 justify-content: center; 204 align-items: center; 205} 206toolbar-item{ 207 font-size: 35px; 208} 209``` 210 211 212```js 213// xxx.js 214import prompt from '@system.prompt'; 215export default { 216 data:{ 217 active: 0, 218 imgList:["common/images/1.png","common/images/2.png","common/images/3.png","common/images/4.png"], 219 itemList:[ 220 {option:'item1',icon:'common/images/1.png'}, 221 {option:'item2',icon:'common/images/2.png'}, 222 {option:'item3',icon:'common/images/3.png'}, 223 {option:'item4',icon:'common/images/4.png'}, 224 ] 225 }, 226 itemClick(id){ 227 this.active= id; 228 }, 229} 230``` 231 232![en-us_image_0000001231683128](figures/en-us_image_0000001231683128.gif) 233