文档管理中心

您当前浏览的3.1/4.0版本文档归档不再维护,推荐您使用最新的HarmonyOS NEXT版本文档。详细请参考文档维护策略变更

menu开发指导

提供菜单组件,作为临时性弹出窗口,用于展示用户可执行的操作,具体用法请参考menu

创建menu组件

在pages/index目录下的hml文件中创建一个menu组件,添加target、type、title属性。

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml-->
  2. <div class="container">
  3. <text class="title-text" id="textId">show menu</text>
  4. <menu target="textId" type="click" title="title">
  5. <option value="Item 1">Item 1</option>
  6. <option value="Item 2">Item 2</option>
  7. <option value="Item 3">Item 3</option>
  8. </menu>
  9. </div>
收起
自动换行
深色代码主题
复制
  1. /* xxx.css */
  2. .container{
  3. width: 100%;
  4. height: 100%;
  5. flex-direction: column;
  6. background-color: #F1F3F5;
  7. align-items: center;
  8. justify-content: center;
  9. width: 100%;
  10. }
  11. .title-text{
  12. font-size: 35px;
  13. }

说明
  • menu仅支持option子组件。

  • menu组件不支持focusable、disabled属性。

设置样式

为menu组件设置样式,例如字体颜色、大小、字符间距等。

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml-->
  2. <div class="container">
  3. <text class="title-text" id="textId">show menu</text>
  4. <menu target="textId" type="click" title="title">
  5. <option value="Item 1">Item 1</option>
  6. <option value="Item 2">Item 2</option>
  7. <option value="Item 3">Item 3</option>
  8. </menu>
  9. </div>
收起
自动换行
深色代码主题
复制
  1. /* xxx.css */
  2. .container{
  3. width: 100%;
  4. height: 100%;
  5. flex-direction: column;
  6. background-color: #F1F3F5;
  7. align-items: center;
  8. justify-content: center;
  9. width: 100%;
  10. }
  11. .title-text{
  12. font-size: 35px;
  13. background-color: #5a5aee;
  14. color: white;
  15. width: 70%;
  16. text-align: center;
  17. height: 85px;
  18. border-radius: 12px;
  19. }
  20. menu{
  21. text-color: blue;
  22. font-size: 35px;
  23. letter-spacing: 2px;
  24. }
  25. option{
  26. color: #6a6aef;
  27. font-size: 30px;
  28. }

绑定事件

为menu组件绑定onselected事件(菜单中某个值被点击选中时触发)和oncancel事件(取消操作时触发),点击text组件调用show方法可设置menu组件的坐标。

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml-->
  2. <div class="container">
  3. <text class="title-text" id="textId" onclick="textClick">show menu</text>
  4. <menu title="title" onselected="select" oncancel="cancel" id="menuId">
  5. <option value="Item 1">Item 1</option>
  6. <option value="Item 2">Item 2</option>
  7. <option value="Item 3">Item 3</option>
  8. </menu>
  9. </div>
收起
自动换行
深色代码主题
复制
  1. /* xxx.css */
  2. .container{
  3. width: 100%;
  4. height: 100%;
  5. flex-direction: column;
  6. background-color: #F1F3F5;
  7. width: 100%;
  8. }
  9. .title-text{
  10. font-size: 35px;
  11. background-color: #5a5aee;
  12. color: white;
  13. width: 70%;
  14. text-align: center;
  15. height: 85px;
  16. border-radius: 12px;
  17. margin-top: 500px;
  18. margin-left: 15%;
  19. }
  20. menu{
  21. text-color: blue;
  22. font-size: 35px;
  23. letter-spacing: 2px;
  24. }
  25. option{
  26. color: #6a6aef;
  27. font-size: 30px;
  28. }
收起
自动换行
深色代码主题
复制
  1. // xxx.js
  2. import promptAction from '@ohos.promptAction';
  3. export default {
  4. select(e) {
  5. promptAction.showToast({
  6. message: e.value
  7. })
  8. },
  9. cancel(){
  10. promptAction.showToast({
  11. message: "cancel"
  12. })
  13. },
  14. textClick() {
  15. this.$element("menuId").show({x:175,y:590});
  16. },
  17. }

场景示例

本场景中开发者可点击toggle组件修改文字颜色,选择menu组件修改渐变色块大小。

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml-->
  2. <div class="container">
  3. <div class="contentToggle">
  4. <toggle class="toggle" for="{{item in togglesList}}" onclick="toggleClick({{$idx}})" checked="{{item.checked}}">{{item.name}}</toggle>
  5. </div>
  6. <text class="size" style="color: {{color}};">width:{{width}},height:{{height}}</text>
  7. <div style="width: {{width}}px;height: {{height}}px; background-color: cornflowerblue;"></div>
  8. <text id="menuId" class="text">change size</text>
  9. <menu onselected="select" oncancel="cancel" target="menuId">
  10. <option value="{{item.value}}" for="item in optionList">{{item.text}}</option>
  11. </menu>
  12. </div>
收起
自动换行
深色代码主题
复制
  1. /* xxx.css */
  2. .container{
  3. flex-direction: column;
  4. background-color: #F1F3F5;
  5. width: 100%;
  6. justify-content: center;
  7. align-items: center;
  8. }
  9. .contentToggle{
  10. width: 100%;
  11. justify-content: space-around;
  12. }
  13. .toggle{
  14. padding: 10px;
  15. height:80px;
  16. font-size: 35px;
  17. width: 200px;
  18. height: 85px;
  19. }
  20. .size{
  21. width: 100%;
  22. height: 200px;
  23. text-align: center;
  24. font-size: 40px;
  25. text-align: center;
  26. }
  27. .text{
  28. width: 300px;
  29. height: 80px;
  30. background-color: #615def;
  31. color: white;
  32. font-size: 35px;
  33. text-align: center;
  34. margin-top: 100px;
  35. }
  36. menu{
  37. text-color: blue;
  38. font-size: 35px;
  39. letter-spacing: 2px;
  40. }
  41. option{
  42. color: #6a6aef;
  43. font-size: 30px;
  44. }
收起
自动换行
深色代码主题
复制
  1. // xxx.js
  2. export default {
  3. data:{
  4. fresh: false,
  5. width: 200,
  6. height: 200,
  7. color: '',
  8. optionList:[
  9. {text:'200 x 200',value:2},
  10. {text:'300 x 300',value:3},
  11. {text:'500 x 500',value:5},
  12. ],
  13. togglesList:[
  14. {name:"red", checked:false},
  15. {name:"blue", checked:false},
  16. {name: "black", checked:false},
  17. ],
  18. },
  19. toggleClick(index) {
  20. for(let i=0;i<this.togglesList.length;i++) {
  21. if (i == index) {
  22. this.color = this.togglesList[index].name;
  23. this.togglesList[i].checked = true;
  24. }else {
  25. this.togglesList[i].checked = false;
  26. }
  27. }
  28. },
  29. select(e) {
  30. this.width = e.value * 100;
  31. this.height = e.value * 100;
  32. }
  33. }

在 指南 中进行搜索
请输入您想要搜索的关键词