First, set a class CustomMenuItemRender to inherit MenuItemRenderer with a parameter added as the component of customized Icon.
private var image:UIComponent = new UIComponent();Then rewrite the method “measure” (to calculate the width and height of “MenuItem”).
override protected function measure():void {Rewrite the method “commitProperties” (Rewrite and add Icon & set the width and height of Icon).
super.measure();
if (separatorIcon || listData == null) {
return;
}
var imageAsset:IImageAsset = Utils.getImageAsset(data.@iconName);
if(imageAsset == null){
return;
}
measuredWidth += imageAsset.width;
if(imageAsset.height > measuredHeight){
measuredHeight = imageAsset.height;
}
}
override protected function commitProperties():void {Rewrite the method “updateDisplayList”(specify the position of Icon: since Icon is at the left side, so we’d better first call the method “super” before the movement of Label):
super.commitProperties();
if (separatorIcon || listData == null) {
return;
}
var imageAsset:IImageAsset = Utils.getImageAsset(data.@iconName);
if(imageAsset == null){
return;
}
image.width = imageAsset.width;
image.height = imageAsset.height;
image.graphics.beginBitmapFill(imageAsset.getBitmapData());
image.graphics.drawRect(0, 0, image.width, image.height);
image.graphics.endFill();
if(!this.contains(image)){
this.addChild(image);
}
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{Rewrite the method “measuredIconWidth” (to calculate the width of Icon):
super.updateDisplayList(unscaledWidth, unscaledHeight);
if (separatorIcon || listData == null) {
return;
}
var imageAsset:IImageAsset = Utils.getImageAsset(data.@iconName);
if(imageAsset == null){
return;
}
if(typeIcon){
typeIcon.x += imageAsset.width;
}
if(label){
label.x += imageAsset.width;
}
}
override public function get measuredIconWidth():Number {At last, specify “ItemRenderer” of Menu with customized CustomMenuItemRenderer. Notice that the name of icon specified by iconName should be used. (Here is the name of the registered image in TWawer.) Also, other names can be used. Do not forget to change “@iconNme” in “CustomMenuItemRenderer”.
var imageAsset:IImageAsset = Utils.getImageAsset(data.@iconName);
if(imageAsset == null){
return 0 ;
}else{
var horizontalGap:Number = getStyle("horizontalGap");
return imageAsset.width + horizontalGap;
}
}
var menu:Menu = Menu.createMenu(network, myMenuData, false);Specify the XML file of the data in Menu as follows:
menu.labelField = "@label";
menu.itemRenderer = new ClassFactory(CustomMenuItemRenderer);
var point:Point = network.getLogicalPoint(event.mouseEvent);
network.callLater(function():void{
menu.show(point.x, point.y);
});
<mx:XML format="e4x" id="myMenuData">Please see the official document for more on the method MenuItemRenderer:
<root>
<menuitem label="www.servasoftware.com" iconName="databox_icon">
<menuitem label="TWaver" type="check" toggled="true">
<menuitem label="Java" type="radio" groupName="one"/>
<menuitem label="Web" type="radio" groupName="one" toggled="true"/>
<menuitem label="Flex" type="radio" groupName="one" iconName="bus_icon"/>
<menuitem label="Silverlight" type="radio" groupName="one"/>
</menuitem>
<menuitem type="separator"/>
<menuitem label="2BizBox" iconName="data_icon"/>
</menuitem>
<menuitem label="www.2bizbox.com"/>
<menuitem label="twaver.servasoft.com"/>
</root>
</mx:XML>
</code>
http://livedocs.adobe.com/flex/3/html/help.html?content=menucontrols_3.htmlhttp://livedocs.adobe.com/flex/3/html/help.html?content=menucontrols_3.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/menuClasses/MenuItemRenderer.html
No comments:
Post a Comment