Flex 3:带有PopUpButton的圆形菜单

时间:2009-09-10 23:48:43

标签: flex flex3

所以我有一个PopupButton,当我点击按钮时,我希望弹出的菜单有圆角。我该怎么做呢?

更新: 我找到了类似于我想要做的更新,可以在以下页面找到:

http://blog.flexmonkeypatches.com/2007/10/08/flex-rounded-menues-using-masking/comment-page-1/

唯一的区别是我用PopUpButton显示菜单。到目前为止,这是我的自定义菜单:

package {

    import flash.display.Sprite;

    import mx.controls.Menu;
    import mx.events.MenuEvent;

    public class MyMenu extends Menu {

        public function MyMenu() {
            super();
            addEventListener("menuShow", onMenuShow);
        }

        private function onMenuShow(e:MenuEvent):void {
            callLater(maskRoundedCorners,[e]);
        }


     private function maskRoundedCorners(e:MenuEvent):void {

                var menu:Menu = e.menu as Menu;
                menu.cacheAsBitmap=false;

                if (!menu.mask){
                    var maskx:uint = menu.x;
                    var masky:uint = menu.y;
                    var maskw:uint = menu.getExplicitOrMeasuredWidth();
                    var maskh:uint = menu.getExplicitOrMeasuredHeight();
                    var rad:int = menu.getStyle("cornerRadius") * 2;

                    var roundRect:Sprite = new Sprite();
                    roundRect.graphics.beginFill(0xFFFFFF);
                    roundRect.graphics.drawRoundRect(maskx,masky,maskw,maskh,rad);
                    roundRect.graphics.endFill();
                    menu.mask = roundRect;
            }
        }  
    }
}

任何人都可以帮助我解决我所缺少的问题......我不确定是否需要覆盖任何菜单类功能。

当我加载我的示例时:

addEventListener(“menuShow”,onMenuShow);

起火但是当我点击PopUpButton来显示菜单时,onMenuShow函数没有被触发,常规菜单显示没有圆角。

对此有任何帮助表示赞赏。

谢谢

1 个答案:

答案 0 :(得分:1)

终于弄明白了。以下是我提出的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <mx:Script>
        <![CDATA[
            import mx.controls.Menu;

            [Bindable]
            private var myMenu:Menu;

            private function popUpButtonInit():void {
                myMenu = new Menu();
                myMenu.dataProvider = arr;

            }

            private function menuOpenHandler():void {
                    var maskx:uint = myMenu.x; 
                    var masky:uint = myMenu.y - 1; 
                    var maskw:uint = myMenu.getExplicitOrMeasuredWidth(); 
                    var maskh:uint = myMenu.getExplicitOrMeasuredHeight(); 
                    var rad:int = myMenu.getStyle("cornerRadius");

                    var roundRect:Sprite = new Sprite();
                    roundRect.graphics.beginFill( 0xFFFFFF );
                    roundRect.graphics.drawRoundRect( maskx, masky, maskw, maskh, rad ); 
                    roundRect.graphics.endFill(); 
                    myMenu.mask = roundRect;

            }           


        ]]>
    </mx:Script>

    <mx:Style>
        Menu {
            corner-radius: 30;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="Alert" />
        <mx:Object label="Button" />
        <mx:Object label="ButtonBar" />
        <mx:Object label="CheckBox" />
        <mx:Object label="ColorPicker" />
        <mx:Object label="ComboBox" />
    </mx:Array>

    <mx:PopUpButton width="78" height="25"  
                    initialize="popUpButtonInit();" 
                    popUp="{myMenu}" popUpGap="3" open="menuOpenHandler();" 
                    horizontalCenter="0" verticalCenter="0">
    </mx:PopUpButton>

</mx:Application>

如果您碰巧有更好的解决方案,请发表评论。