2011-10-14 111 views
0

我已經搜查了網,但所有這些TUTS是Flex 3中的Flex 4.5對齊菜單項中心

什麼是Flex 4.5中的approcach對準在酒吧的中間mx:MenuBar項目?

(通常他們是在最左側)

+0

MX組件是Flex 3個的組件,並且您在此找到教程可能提供正確的解決方案。 Flex 4中的新功能是Spark組件。 – JeffryHouser

回答

0

flexdeveloper.eu,設置itemAlign中心

package custom{ 
    import flash.geom.Rectangle; 

    import mx.controls.MenuBar; 
    import mx.controls.menuClasses.IMenuBarItemRenderer; 
    import mx.core.IFlexDisplayObject; 

    public class AlignableMenuBar extends MenuBar { 
     private static const MARGIN_WIDTH:int=10; 
     private var background:IFlexDisplayObject; 
     public var itemAlign:String; 

     public function AlignableMenuBar() { 
      super(); 
     } 

     override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void { 
      if (this.itemAlign == "right") { 
       updateDisplayListRightAlign(unscaledWidth,unscaledHeight); 
      } else if (this.itemAlign == "center") { 
       updateDisplayListCenterAlign(unscaledWidth,unscaledHeight); 
      } else { 
       updateDisplayListLeftAlign(unscaledWidth,unscaledHeight); 
      } 
     } 

     protected function updateDisplayListLeftAlign(unscaledWidth:Number,unscaledHeight:Number):void { 
      super.updateDisplayList(unscaledWidth,unscaledHeight); 

      var lastX:Number=MARGIN_WIDTH; 
      var lastW:Number=0; 
      var len:int=menuBarItems.length; 

      var clipContent:Boolean=false; 
      var hideItems:Boolean=unscaledWidth == 0 || unscaledHeight == 0; 

      for (var i:int=0; i < len; i++) { 
       var item:IMenuBarItemRenderer=menuBarItems[i]; 

       item.setActualSize(item.getExplicitOrMeasuredWidth(),unscaledHeight); 
       item.visible=! hideItems; 

       lastX=item.x=lastX + lastW; 
       lastW=item.width; 

       if (! hideItems && item.getExplicitOrMeasuredHeight() > unscaledHeight || lastX + lastW > unscaledWidth) { 
        clipContent=true; 
       } 
      } 

      if (background) { 
       background.setActualSize(unscaledWidth,unscaledHeight); 
       background.visible=! hideItems; 
      } 

      // Set a scroll rect to handle clipping. 
      scrollRect=clipContent?new Rectangle(0,0,unscaledWidth,unscaledHeight):null; 

     } 

     protected function updateDisplayListCenterAlign(unscaledWidth:Number,unscaledHeight:Number):void { 
      super.updateDisplayList(unscaledWidth,unscaledHeight); 

      var len:int=menuBarItems.length; 

      var totalWidth:int=0; 
      for (var i:int=0; i < len; i++) { 
       var tempItem:IMenuBarItemRenderer=menuBarItems[i]; 
       totalWidth+= tempItem.width; 
      } 
      var lastX:Number=(this.width - totalWidth)/2; 
      var lastW:Number=0; 

      var clipContent:Boolean=false; 
      var hideItems:Boolean=unscaledWidth == 0 || unscaledHeight == 0; 

      for (var j:int=0; j < len; j++) { 
       var item:IMenuBarItemRenderer=menuBarItems[j]; 

       item.setActualSize(item.getExplicitOrMeasuredWidth(),unscaledHeight); 
       item.visible=! hideItems; 

       lastX=item.x=lastX + lastW; 

       lastW=item.width; 

       if (! hideItems && item.getExplicitOrMeasuredHeight() > unscaledHeight || lastX + lastW > unscaledWidth) { 
        clipContent=true; 
       } 
      } 

      if (background) { 
       background.setActualSize(unscaledWidth,unscaledHeight); 
       background.visible=! hideItems; 
      } 

      // Set a scroll rect to handle clipping. 
      scrollRect=clipContent?new Rectangle(0,0,unscaledWidth,unscaledHeight):null; 


     } 

     protected function updateDisplayListRightAlign(unscaledWidth:Number,unscaledHeight:Number):void { 
      super.updateDisplayList(unscaledWidth,unscaledHeight); 

      var len:int=menuBarItems.length; 

      var totalWidth:int=0; 
      for (var i:int=0; i < len; i++) { 
       var tempItem:IMenuBarItemRenderer=menuBarItems[i]; 
       totalWidth+= tempItem.width; 
      } 
      var lastX:Number=this.width - totalWidth; 
      var lastW:Number=0; 

      var clipContent:Boolean=false; 
      var hideItems:Boolean=unscaledWidth == 0 || unscaledHeight == 0; 

      for (var j:int=0; j < len; j++) { 
       var item:IMenuBarItemRenderer=menuBarItems[j]; 

       item.setActualSize(item.getExplicitOrMeasuredWidth(),unscaledHeight); 
       item.visible=! hideItems; 

       lastX=item.x=lastX + lastW; 

       lastW=item.width; 

       if (! hideItems && item.getExplicitOrMeasuredHeight() > unscaledHeight || lastX + lastW > unscaledWidth) { 
        clipContent=true; 
       } 
      } 

      if (background) { 
       background.setActualSize(unscaledWidth,unscaledHeight); 
       background.visible=! hideItems; 
      } 

      // Set a scroll rect to handle clipping. 
      scrollRect=clipContent?new Rectangle(0,0,unscaledWidth,unscaledHeight):null; 
     } 
    } 
} 
0

你爲什麼不只是把你的菜單欄在父容器?父容器將具有100%的寬度,而MenuBar不會。 MenuBar然後可以在該容器內水平放置。

+0

不是酒吧,物品!! – yolo