W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
為組件綁定彈出式菜單,彈出式菜單以垂直列表形式顯示菜單項(xiàng),可通過長按、點(diǎn)擊或鼠標(biāo)右鍵觸發(fā)。
名稱 | 參數(shù)類型 | 描述 |
---|---|---|
bindMenu | Array<MenuItem> | CustomBuilder | 給組件綁定菜單,點(diǎn)擊后彈出菜單。彈出菜單項(xiàng)支持文本和自定義兩種功能。 |
bindContextMenu8+ | content: CustomBuilder, responseType: ResponseType | 給組件綁定菜單,觸發(fā)方式為長按或者右鍵點(diǎn)擊,彈出菜單項(xiàng)需要自定義。 |
普通菜單
- // xxx.ets
- @Entry
- @Component
- struct MenuExample {
- build() {
- Column() {
- Text('click for Menu')
- }
- .width('100%')
- .margin({ top: 5 })
- .bindMenu([
- {
- value: 'Menu1',
- action: () => {
- console.info('handle Menu1 select')
- }
- },
- {
- value: 'Menu2',
- action: () => {
- console.info('handle Menu2 select')
- }
- },
- ])
- }
- }
自定義內(nèi)容菜單
- @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, index) => {
- Column() {
- Row() {
- Image($r("app.media.icon")).width(20).height(20).margin({ right: 5 })
- Text(`Menu${index + 1}`).fontSize(20)
- }
- .width('100%')
- .height(30)
- .justifyContent(FlexAlign.Center)
- .align(Alignment.Center)
- .onClick(() => {
- console.info(`Menu${index + 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)
- }
- .height('100%')
- .width('100%')
- .backgroundColor('#f0f0f0')
- }
- }
菜單(右鍵觸發(fā)顯示)
- // xxx.ets
- @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('rightclick for menu')
- }
- .width('100%')
- .margin({ top: 5 })
- .bindContextMenu(this.MenuBuilder, ResponseType.RightClick)
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: