为组件绑定弹出式菜单,支持长按、点击或鼠标右键来触发菜单的弹出,菜单项以垂直列表形式显示。菜单控制适用于需要提供快捷操作选项、设置选项或上下文相关操作的场景,为用户提供便捷的操作入口,提升应用交互体验。支持自定义菜单内容和样式,灵活适配不同业务需求。
从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
不支持在CustomBuilder中使用bindMenu和bindContextMenu弹出多级菜单。对此,可以使用Menu组件来实现多级菜单。
弹出菜单的文本内容不支持长按选中。
当窗口大小发生变化以及点击菜单内容区时,菜单自动隐藏。
如果绑定菜单的组件是可拖动节点且未指定bindContextMenu的preview,菜单弹出时会显示拖拽预览图,且菜单选项和预览图不会相互避让。开发者可根据使用场景设置preview或将目标节点设置为不可拖动。
从API version 12开始,菜单支持长按500ms弹出子菜单,支持按压态跟随手指移动。
仅支持使用Menu组件且子组件包含MenuItem或MenuItemGroup的场景。
仅支持MenuPreviewMode设置为NONE的菜单。
菜单最大宽度受设备所占栅格限制,即使设置宽度100%,也不会占满屏幕。
菜单绑定的组件对象销毁时,菜单消失。
bindContextMenu仅支持在子窗中显示,bindMenu可以通过配置MenuOptions中的showInSubWindow属性设置是否在子窗中显示。
type AnimationRange<T>=[from: T, to: T]
动画开始和结束时相对预览原图缩放比例。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
元服务API: 从API version 12开始,该接口支持在元服务中使用。
| 类型 | 说明 |
|---|---|
| [from: T, to: T] | from表示动画开始时相对预览原图缩放比例,to表示动画结束时相对预览原图缩放比例。 |
菜单弹出时振动效果。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 值 | 说明 |
|---|---|---|
| DISABLED | 0 | 菜单弹出时不振动。 |
| ENABLED | 1 | 菜单弹出时振动。 |
| AUTO | 2 | 菜单振动效果跟随系统,当前为菜单有蒙层时振动。 |
type BorderRadiusType = Length | BorderRadiuses | LocalizedBorderRadiuses
圆角类型。
元服务API: 从API version 19开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Length | 长度类型,用于描述尺寸单位。 |
| BorderRadiuses | 圆角类型,用于描述组件边框圆角半径。 |
| LocalizedBorderRadiuses | 圆角类型,用于描述组件边框圆角半径。 |
子窗菜单的模态模式。
元服务API: 从API version 20开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 值 | 说明 |
|---|---|---|
| AUTO | 0 | 自动模式,菜单组件在当前设备的默认行为。当前版本在所有设备上的效果等同于ModalMode.NONE。 |
| NONE | 1 | 除菜单自身区域外,其他区域均可传递事件,下层控件可响应事件。 |
| TARGET_WINDOW | 2 | 菜单所在应用的窗口与菜单区域不可传递事件,其他区域可传递事件。 |
预览图的缩放方式。
元服务API: 从API version 20开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 值 | 说明 |
|---|---|---|
| AUTO | 0 | 预览图根据Placement自动调整预览图宽高及缩放。 |
| CONSTANT | 1 | 预览图不缩放,大小保持不变。最终仍会受到安全区的限制而出现压缩、裁剪。 |
| MAINTAIN | 2 | 预览图缩放时保持宽高比。 |
预览图宽高设置为百分比时的参考可布局区域大小。
元服务API: 从API version 20开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 值 | 说明 |
|---|---|---|
| SAFE_AREA | 0 | 参考可布局区域大小为窗口大小减去上下左右安全边距。 |
该示例为bindMenu通过配置MenuElement弹出普通菜单。
@Entry
@Component
struct MenuExample {
build() {
Column() {
Text('click for Menu')
.bindMenu([
{
value: 'Menu1',
action: () => {
console.info('handle Menu1 select');
}
},
{
value: 'Menu2',
action: () => {
console.info('handle Menu2 select');
}
},
])
}
.width('100%')
.margin({ top: 5 })
}
} 
该示例为bindMenu通过配置CustomBuilder弹出自定义菜单。同时,从API version 18开始支持通过配置ContextMenuOptions中的hapticFeedbackMode属性实现菜单弹出时的振动效果。
@Entry
@Component
struct MenuExample {
@State listData: number[] = [0, 0, 0];
@Builder MenuBuilder() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
ForEach(this.listData, (item:number, index) => {
Column() {
Row() {
// $r('app.media.icon')需要替换为开发者所需的图像资源文件。
Image($r("app.media.icon")).width(20).height(20).margin({ right: 5 })
Text(`Menu${index as number + 1}`).fontSize(20)
}
.width('100%')
.height(30)
.justifyContent(FlexAlign.Center)
.align(Alignment.Center)
.onClick(() => {
console.info(`Menu${index as number + 1} Clicked!`);
})
if (index != this.listData.length - 1) {
Divider().height(10).width('80%').color('#ccc')
}
}.padding(5).height(40)
})
}.width(100)
}
build() {
Column() {
Text('click for menu')
.fontSize(20)
.margin({ top: 20 })
.bindMenu(this.MenuBuilder, { hapticFeedbackMode: HapticFeedbackMode.ENABLED })
}
.height('100%')
.width('100%')
.backgroundColor('#f0f0f0')
}
} 
该示例为bindContextMenu通过配置responseType.LongPress弹出菜单。
@Entry
@Component
struct ContextMenuExample {
@Builder MenuBuilder() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
Text('Test menu item 1')
.fontSize(20)
.width(100)
.height(50)
.textAlign(TextAlign.Center)
Divider().height(10)
Text('Test menu item 2')
.fontSize(20)
.width(100)
.height(50)
.textAlign(TextAlign.Center)
}.width(100)
}
build() {
Column() {
Text('LongPress for menu')
}
.width('100%')
.margin({ top: 5 })
.bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
}
} 
该示例为bindContextMenu通过配置responseType.RightClick和ContextMenuOptions中的enableArrow属性弹出指向型菜单。同时,从API version 18开始支持通过配置ContextMenuOptions中的hapticFeedbackMode属性实现菜单弹出时的振动效果。
@Entry
@Component
struct DirectiveMenuExample {
@Builder MenuBuilder() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('Options')
Divider().strokeWidth(2).margin(5).color('#F0F0F0')
Text('Hide')
Divider().strokeWidth(2).margin(5).color('#F0F0F0')
Text('Exit')
}
.width(200)
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Column() {
Text("DirectiveMenuExample")
.fontSize(20)
.width('100%')
.height("25%")
.backgroundColor('#F0F0F0')
.textAlign(TextAlign.Center)
.bindContextMenu(this.MenuBuilder, ResponseType.RightClick, {
enableArrow: true,
placement: Placement.Bottom,
hapticFeedbackMode: HapticFeedbackMode.ENABLED
})
}
}
.width('100%')
.height('100%')
}
} 
该示例为bindContextMenu通过配置responseType.LongPress和ContextMenuOptions中preview属性的MenuPreviewMode类型弹出菜单预览样式。
@Entry
@Component
struct Index {
// $r('app.media.icon')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r("app.media.icon");
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Text('preview-image')
.width(200)
.height(100)
.textAlign(TextAlign.Center)
.margin(100)
.fontSize(30)
.bindContextMenu(this.MyMenu, ResponseType.LongPress,
{ preview: MenuPreviewMode.IMAGE,
previewAnimationOptions: {scale: [0.8, 1.0]},
})
.backgroundColor("#ff3df2f5")
}
}.width('100%')
}
}
} 
该示例为bindContextMenu通过配置responseType.LongPress和ContextMenuOptions中preview属性的CustomBuilder类型弹出菜单自定义预览样式。
@Entry
@Component
struct Index {
// $r('app.media.icon')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r("app.media.icon");
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
}
}
@Builder
MyPreview() {
Column() {
Image($r('app.media.icon'))
.width(200)
.height(200)
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Text('preview-builder')
.width(200)
.height(100)
.textAlign(TextAlign.Center)
.margin(100)
.fontSize(30)
.bindContextMenu(this.MyMenu, ResponseType.LongPress,
{
preview: this.MyPreview
})
}
}.width('100%')
}
}
} 
该示例为bindContextMenu通过配置isShown弹出菜单预览样式。
@Entry
@Component
struct Index {
// $r('app.media.icon')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r("app.media.icon");
@State isShown: boolean = false;
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
}
}
@Builder
MyPreview() {
Column() {
Image($r('app.media.icon'))
.width(200)
.height(200)
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Text('preview-builder')
.width(200)
.height(100)
.textAlign(TextAlign.Center)
.margin(100)
.fontSize(30)
.bindContextMenu(this.isShown, this.MyMenu,
{
preview: this.MyPreview,
aboutToDisappear: ()=>{
this.isShown = false;
}
})
Button('click')
.onClick(()=>{
this.isShown = true;
})
}
}.width('100%')
}
}
} 
该示例为bindContextMenu通过配置ContextMenuOptions中的transition属性,实现自定义菜单以及菜单预览时的显示和退出动效。
@Entry
@Component
struct MenuExample {
@Builder
MenuBuilder() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
Text('Menu item 1')
.fontSize(12)
.width(200)
.height(30)
.textAlign(TextAlign.Center)
Divider().height(10)
Text('Menu item 2')
.fontSize(12)
.width(100)
.height(30)
.textAlign(TextAlign.Center)
}.width(100)
}
@Builder
MyPreview() {
Column() {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
Image($r('app.media.startIcon'))
.width(50)
.height(50)
}
}
build() {
Column() {
Button('LongPress bindContextMenu')
.margin({ top: 15 })
.bindContextMenu(
this.MenuBuilder,
ResponseType.LongPress, {
transition: TransitionEffect.OPACITY.animation({ duration: 4000, curve: Curve.Ease }).combine(
TransitionEffect.rotate({ z: 1, angle: 180 })),
preview: this.MyPreview,
previewAnimationOptions: {
scale: [0.8, 1.0],
transition: TransitionEffect.OPACITY.animation({ duration: 4000, curve: Curve.Ease }).combine(
TransitionEffect.rotate({ z: 1, angle: 180 }))
}
})
}
.width('100%')
.margin({ top: 5 })
}
} 
该示例为bindMenu通过配置MenuElement的symbolIcon弹出菜单。
import { SymbolGlyphModifier } from '@kit.ArkUI';
@Entry
@Component
struct MenuExample {
@State symbolIconModifier1: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_photo')).fontSize('24vp');
@State symbolIconModifier2: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_photo')).fontSize('24vp');
build() {
Column() {
Text('click for Menu')
}
.width('100%')
.margin({ top: 5 })
.bindMenu([
{
value: 'Menu1',
symbolIcon:this.symbolIconModifier1,
action: () => {
console.info('handle Menu1 select');
}
},
{
value: 'Menu2',
symbolIcon:this.symbolIconModifier2,
action: () => {
console.info('handle Menu2 select');
}
},
])
}
} 
该示例为bindContextMenu通过配置ContextMenuOptions中previewAnimationOptions属性的hoverScale,实现组件截图到自定义预览图的一镜到底过渡动效。
@Entry
@Component
struct Index {
// $r('app.media.xxx')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r("app.media.app_icon");
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: "Menu option" })
MenuItem({ startIcon: this.iconStr, content: "Menu option" })
MenuItem({ startIcon: this.iconStr, content: "Menu option" })
}
}
@Builder
MyPreview() {
Column() {
Image($r('app.media.example'))
.width(200)
.height(200)
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Image($r('app.media.example'))
.width(100)
.height(100)
.margin(100)
.bindContextMenu(this.MyMenu, ResponseType.LongPress,
{
preview: this.MyPreview,
previewAnimationOptions: {
hoverScale: [1.0, 0.95]
}
})
}
}.width('100%')
}
}
} 
该示例为bindMenu通过配置ContextMenuOptions中的backgroundBlurStyleOptions属性,实现了自定义菜单背景模糊效果。
从API version 18开始,在ContextMenuOptions中新增了backgroundBlurStyleOptions属性。
@Entry
@Component
struct MenuExample {
build() {
Stack() {
// $r('app.media.bg')需要替换为开发者所需的图像资源文件。
Image($r('app.media.bg'))
Column() {
Text('click for Menu')
.bindMenu([
{
value: 'Menu1',
action: () => {
console.info('handle Menu1 select')
}
},
{
value: 'Menu2',
action: () => {
console.info('handle Menu2 select')
}
},
],
{
backgroundBlurStyle: BlurStyle.BACKGROUND_THIN,
backgroundBlurStyleOptions: {
colorMode: ThemeColorMode.LIGHT,
blurOptions: { grayscale: [20, 20] },
policy: BlurStyleActivePolicy.ALWAYS_ACTIVE,
adaptiveColor: AdaptiveColor.AVERAGE,
scale: 1
},
}
)
}
.width('100%')
.margin({ top: 5 })
}
}
} 
该示例为bindMenu通过配置ContextMenuOptions中的backgroundEffect属性,实现了自定义菜单背景效果。
从API version 18开始,在ContextMenuOptions中新增了backgroundEffect属性。
@Entry
@Component
struct MenuExample {
build() {
Stack() {
// $r('app.media.bg')需要替换为开发者所需的图像资源文件。
Image($r('app.media.bg'))
Column() {
Text('click for Menu')
.bindMenu([
{
value: 'Menu1',
action: () => {
console.info('handle Menu1 select');
}
},
{
value: 'Menu2',
action: () => {
console.info('handle Menu2 select');
}
},
],
{
backgroundBlurStyle: BlurStyle.BACKGROUND_THIN,
backgroundEffect: {
radius: 60,
saturation: 10,
brightness: 1,
color: '#661A1A1A',
adaptiveColor: AdaptiveColor.AVERAGE,
blurOptions:{grayscale:[20,20]}
}
}
)
}
.width('100%')
.margin({ top: 5 })
}
}
} 
该示例通过为bindContextMenu配置ContextMenuOptions中的previewAnimationOptions属性实现了一镜到底过渡动效的同时,再配置hoverScaleInterruption控制是否允许长按抬手取消菜单弹出。
从API version 20开始,在previewAnimationOptions的类型ContextMenuAnimationOptions中新增了hoverScaleInterruption属性。
@Entry
@Component
struct Index {
// $r('app.media.xxx')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r("app.media.app_icon");
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
}
}
@Builder
MyPreview() {
Column() {
Image($r('app.media.example'))
.width(300)
.height(200)
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Image($r('app.media.example'))
.width(100)
.height(100)
.margin(100)
.bindContextMenu(this.MyMenu, ResponseType.LongPress,
{
preview: this.MyPreview,
previewAnimationOptions: {
hoverScale: [1.0, 0.8],
hoverScaleInterruption: true
}
})
.onClick(() => {
console.info('trigger onClick')
})
}
}.width('100%')
}
}
} 
该示例通过bindContextMenu配置responseType.LongPress来实现功能。同时,在ContextMenuOptions中配置preview属性的MenuPreviewMode类型来设置菜单预览样式。最后,通过设置previewBorderRadius来实现预览图边框的圆角半径。
从API version 19开始,在ContextMenuOptions中新增了previewBorderRadius属性。
@Entry
@Component
struct Index {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r("app.media.startIcon");
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
MenuItem({ startIcon: this.iconStr, content: "菜单选项" })
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Text('preview-image')
.width(200)
.height(100)
.textAlign(TextAlign.Center)
.margin(100)
.fontSize(30)
.bindContextMenu(this.MyMenu, ResponseType.LongPress,
{
preview: MenuPreviewMode.IMAGE,
previewBorderRadius: 50
})
.backgroundColor("#ff7fcdff")
}
}.width('100%')
}
}
} 
该示例为bindMenu通过配置mask属性设置菜单蒙层。
从API version 20开始,在ContextMenuOptions中新增了mask属性。
import { SymbolGlyphModifier } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State startIconModifier: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_star'))
@State isShow: boolean = false;
@Builder
MyMenu() {
Menu() {
MenuItem({
symbolStartIcon: this.startIconModifier,
content: "新建文件夹",
})
MenuItem({
symbolStartIcon: this.startIconModifier,
content: "排序方式",
})
MenuItem({
symbolStartIcon: this.startIconModifier,
content: "查看方式",
})
}
}
build() {
Button('bindMenu')
.position({ top: 80, left: 80 })
.onClick(() => {
this.isShow = !this.isShow;
})
.bindMenu(this.isShow, this.MyMenu, {
mask: { color: 'rgba(23,169,141,0.5)', backgroundBlurStyle: BlurStyle.Thin }
})
}
} 
该示例通过在bindContextMenuWithResponse中传入CustomBuilderT<ResponseType>给目标组件绑定菜单,组件会在UI函数中返回弹出菜单的触发方式,开发者可根据返回的触发方式实现差异化显示。
从API version 23开始,新增了bindContextMenuWithResponse的接口。
@Entry
@Component
struct Index {
@State longPress: string = 'LONG_PRESS';
@State rightClick: string = 'RIGHT_CLICK';
@Builder
MenuBuilderWithParam(type: ResponseType) {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
Text('Current ResponseType = ' + (type === ResponseType.RightClick ? 'RIGHT_CLICK' : 'LONG_PRESS'))
Divider().height(10)
if (type === ResponseType.LongPress) {
Text('Item: ' + this.longPress)
.fontSize(20)
.width(200)
.height(20)
.textAlign(TextAlign.Center)
}
if (type === ResponseType.RightClick) {
Text('Item: ' + this.rightClick)
.fontSize(20)
.width(200)
.height(20)
.textAlign(TextAlign.Center)
}
}
}
build() {
Stack() {
Button('BindContextMenu长按和右键点击触发菜单')
.bindContextMenuWithResponse(this.MenuBuilderWithParam, {
enableArrow: true,
})
}
.height('100%')
.width('100%')
.backgroundColor('#f0f0f0')
}
} 
该示例通过在bindMenu中配置keyboardAvoidMode设置菜单避让软键盘,通过minKeyboardAvoidDistance设置菜单避让软键盘的最小距离。
从API version 23开始,ContextMenuOptions中新增keyboardAvoidMode、minKeyboardAvoidDistance属性。
import { inputMethod } from '@kit.IMEKit';
import { LengthMetrics } from '@kit.ArkUI';
@Entry
@Component
struct Index {
private inputController: inputMethod.InputMethodController = inputMethod.getController();
@Builder
MyMenu() {
Menu() {
MenuItem({ content: 'MenuItemContent' })
MenuItem({ content: 'MenuItemContent' })
MenuItem({ content: 'MenuItemContent' })
MenuItem({ content: 'MenuItemContent' })
MenuItem({ content: 'MenuItemContent' })
}
}
build() {
RelativeContainer() {
Button('Click Show Menu')
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center },
})
.bindMenu(this.MyMenu, {
keyboardAvoidMode: MenuKeyboardAvoidMode.TRANSLATE_AND_RESIZE,
minKeyboardAvoidDistance: LengthMetrics.vp(20)
})
.onClick(() => {
setTimeout(() => {
this.attachAndListener()
}, 2000)
})
}
.height('100%')
.width('100%')
}
async attachAndListener() {
focusControl.requestFocus('Index')
try {
await this.inputController.attach(true, {
inputAttribute: {
textInputType: inputMethod.TextInputType.TEXT,
enterKeyType: inputMethod.EnterKeyType.SEARCH
}
})
} catch (err) {
console.error('Fail to attach')
}
}
} 
该示例通过设置ContextMenuOptions中的anchorPosition属性,实现了菜单相对于绑定组件左上角弹出的效果。
从API version 20开始,在ContextMenuOptions中新增了anchorPosition属性。
@Entry
@Component
struct Index {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r('app.media.startIcon');
@State isShown: boolean = false;
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: '菜单选项' })
MenuItem({ startIcon: this.iconStr, content: '菜单选项' })
MenuItem({ startIcon: this.iconStr, content: '菜单选项' })
}
}
@State menuAnchorPositionIndex: number = 0;
private menuAnchorPositionArray: Array<Position> = new Array<Position>(
{ x: 0, y: 0 },
{ x: 150, y: 0 },
{ x: 0, y: 150 },
{ x: 150, y: 150 },
);
build() {
Column({ space: 50 }) {
Column() {
Column() {
Text('Test Menu AnchorPosition')
.width(500)
.height(100)
.textAlign(TextAlign.Center)
.margin(100)
.fontSize(30)
.bindContextMenu(this.isShown, this.MyMenu,
{
anchorPosition: this.menuAnchorPositionArray[this.menuAnchorPositionIndex],
aboutToDisappear: () => {
this.isShown = false;
}
})
Button('click')
.margin(5)
.onClick(() => {
this.isShown = true;
})
Button('AnchorPosition change')
.margin(5)
.onClick(() => {
this.menuAnchorPositionIndex++;
if (this.menuAnchorPositionIndex >= this.menuAnchorPositionArray.length) {
this.menuAnchorPositionIndex = 0;
}
})
Text('Current x: ' + this.menuAnchorPositionArray[this.menuAnchorPositionIndex]?.x +
' , y: ' + this.menuAnchorPositionArray[this.menuAnchorPositionIndex]?.y)
}
}.width('100%')
}
}
} 
该示例为bindContextMenu通过配置ContextMenuOptions中的maxHeight属性,设置菜单的最大高度。
未设置maxHeight属性时,默认按照菜单的最大高度(可用高度的80%),可展示全部列表项,通过设置最大高度为窗口可用高度的50%时,仅能显示8个列表项。
从API版本26.0.0开始,在ContextMenuOptions中新增了maxHeight属性。
import { LengthMetrics } from '@kit.ArkUI';
@Entry
@Component
struct Index {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
private iconStr: ResourceStr = $r('app.media.startIcon');
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: this.iconStr, content: 'MenuItem1' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem2' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem3' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem4' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem5' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem6' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem7' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem8' })
MenuItem({ startIcon: this.iconStr, content: 'MenuItem9' })
}
}
build() {
Column({ space: 50 }) {
Column() {
Column() {
Text('LongPress-image')
.width(200)
.height(100)
.textAlign(TextAlign.Center)
.margin(100)
.fontSize(30)
.bindContextMenu(this.MyMenu, ResponseType.LongPress,
{
maxHeight: LengthMetrics.percent(0.5)
})
.backgroundColor('#ff7fcdff')
}
}.width('100%')
}
}
} 
该示例通过设置ContextMenuOptions中的targetSpace属性,介绍如何增加菜单与目标组件之间的间距。
从API版本26.0.0开始,在ContextMenuOptions中新增了targetSpace属性。
import { LengthMetrics } from '@kit.ArkUI';
@Entry
@Component
struct Alone {
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: $r('app.media.startIcon'), content: '菜单选项1' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '菜单选项2' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '菜单选项3' })
}
}
build() {
Column() {
Stack() {
Column()
.width(120 + 40 * 2)
.height(120 + 40 * 2)
.borderWidth(2)
.borderColor(Color.Orange)
.borderStyle(BorderStyle.Dotted)
Image($r('app.media.startIcon'))
.width(120)
.height(120)
.bindMenu(this.MyMenu,
{
targetSpace: LengthMetrics.vp(40)
})
}.height('75%')
.width('100%')
}
.height('100%')
.width('100%')
}
} 
该示例通过ContextMenuOptions中的systemMaterial属性设置组件的系统材质,实现了菜单的沉浸光感视效。设置系统材质后,Menu弹出过程中会有非线性形变和边缘流光。
该示例配图为高算力设备强档效果,组件沉浸光感效果会根据设备算力与用户在系统中设置的沉浸光感效果自适应调整,开发者无需额外适配。
从API版本26.0.0开始,在ContextMenuOptions中新增了systemMaterial属性。
import { uiMaterial } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: $r('app.media.startIcon'), content: '菜单选项' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '菜单选项' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '菜单选项' })
}
}
build() {
Stack() {
Button('bindMenu with THICK material')
.bindMenu(this.MyMenu, {
systemMaterial: new uiMaterial.ImmersiveMaterial({
style: uiMaterial.ImmersiveStyle.THICK
})
})
}
.height('100%')
.width('100%')
// 请开发者替换为实际资源文件
.backgroundImage($r("app.media.img"))
}
} 未设置系统材质时:

设置系统材质后:

该示例展示了如何在bindContextMenuByIsShow中使用gridStyle设置栅格菜单样式。通过设置count、horizontalSize和position属性,可以自定义菜单的栅格布局。
从API版本26.0.0开始,新增了bindContextMenuByIsShow的接口;在ContextMenuOptions中新增了gridStyle属性。
@Entry
@Component
struct ContextMenuGridStyleExample {
@State isShown: boolean = false;
@Builder
MyMenu() {
Menu() {
MenuItem({ startIcon: $r('app.media.startIcon'), content: '复制' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '粘贴' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '剪切' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '删除' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '分享' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '全选' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '翻译' })
MenuItem({ startIcon: $r('app.media.startIcon'), content: '收藏' })
}
.width(150)
}
build() {
Column({ space: 20 }) {
Text('bindContextMenuByIsShow grid menu')
.fontSize(20)
.bindContextMenuByIsShow(this.isShown, this.MyMenu, {
gridStyle: {
count: 4,
horizontalSize: 3,
position: MenuGridPosition.BOTTOM
},
onWillDisappear: () => {
this.isShown = false;
},
})
.onClick(() => {
this.isShown = true;
})
}
.width('100%')
.margin({ top: 50 })
}
} 