openharmony 鸿蒙 js-components-basic-menu

2025-06-12 浏览 (1)

menu

NOTE

This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

The <menu> component provides menus as temporary pop-up windows to display operations that can be performed by users.

Required Permissions

None

Child Components

Only the <option> component is supported.

Attributes

In addition to the universal attributes, the following attributes are supported.

NameTypeDefault ValueMandatoryDescription
targetstring-NoTarget element to which the menu is attached. When the target element is clicked, the menu is automatically displayed. The menu is preferentially displayed in the lower right corner of the target element. If the visible space on the right is insufficient, the menu is moved leftward. If the visible space in the lower part is insufficient, the menu is moved upward.
typestringclickNoMode in which the target element triggers the pop-up menu. Available values are as follows:
- click: The pop-up menu is triggered by clicking the target element.
- longpress: The pop-up menu is triggered by long pressing the target element.
titlestring-NoTitle of the menu.

NOTE

The focusable and disabled attributes are not supported.

Styles

The following styles are supported.

NameTypeDefault ValueMandatoryDescription
text-color<color>-NoFont color of the menu.
font-size<length>30pxNoFont size of the menu.
allow-scalebooleantrueNoWhether the font size changes with the system's font size settings.
If the config-changes tag of fontSize is configured for abilities in the config.json file, the setting takes effect without application restart.
letter-spacing<length>0NoCharacter spacing of the menu.
font-stylestringnormalNoFont style of the menu. For details, see font-weight of the <text> component.
font-weightnumber |stringnormalNoFont weight of the menu. For details, see font-weight of the <text> component.
font-familystringsans-serifNoFont family, in which fonts are separated by commas (,). Each font is set using a font name or font family name. The first font in the family or the specified custom font is used for the text.

Events

The following events are supported.

NameParameterDescription
selected{ value:value }Triggered when a value in the menu is selected. The return value is the value attribute of the <option> component.
cancel-Triggered when an operation is canceled by the user.

Methods

The following methods are supported.

NameParameterDescription
show{ x:x, y:y }Displays the menu. x and y specify the position of the displayed menu. x indicates the x coordinate from the left edge of the visible area, and does not include any scrolling offset. y indicates the y coordinate from the upper edge of the visible area, and does not include any scrolling offset or a status bar. The menu is preferentially displayed in the lower right corner of the target element. If the visible space on the right is insufficient, the menu is moved leftward. If the visible space in the lower part is insufficient, the menu is moved upward.

Example

<!-- xxx.hml -->
<div class="container">
  <text onclick="onTextClick" class="title-text">Show popup menu.</text>
  <menu id="apiMenu" onselected="onMenuSelected">
    <option value="Item 1">Item 1</option>
    <option value="Item 2">Item 2</option>
    <option value="Item 3">Item 3</option>
  </menu>
</div>
/* xxx.css */
.container {
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.title-text {
  margin: 20px;
}
// xxx.js
import promptAction from '@ohos.promptAction';
export default {
  onMenuSelected(e) {
    promptAction.showToast({
      message: e.value
    })
  },
  onTextClick() {
    this.$element("apiMenu").show({x:280,y:120});
  }
}

menu13

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Full)

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/LqbK2Z