2012-05-24 34 views
0

我已經完成了大量的研究並閱讀Adobe Live Docs,直到我的眼睛流血,試圖弄清楚這一點。Flex:ActionScript中的自定義複合組件 - 存在百分比/百分比高度的問題

我在構建ActionScript中的組合自定義組件,並希望水平佈局子控件。這可以正常工作,將它們添加到HGroup並將HGroup添加到組件中,問題隨基於百分比的大小而定。

我需要_horizontalGroup:HGroup根據其容器大小自行調整大小。

逐句通過代碼示出了每個也是UIComponent的父屬性是...

  • _horizo​​ntalGroup.parent = ReportGridSelector
  • ReportGridSelector.parent = grpControls

如果grpControls具有顯式大小,不應該ReportGridSelector也有它的大小?

自定義組件是這樣實現的...
注:ReportControl擴展UIComponent的,不包含任何大小的邏輯

public class ReportGridSelector extends ReportControl{ 

    /*other display objects*/ 
    private var _horizontalGroup:HGroup; 

    public function ReportGridSelector(){ 
     super(); 
     percentHeight = 100; 
     percentWidth = 100; 
    } 

    override protected function createChildren():void{ 

     super.createChildren(); 

     if(!_horizontalGroup){ 
      _horizontalGroup = new HGroup(); 

      //I WANT SIZE BY PERCENTAGE, BUT THIS DOESN'T WORK 
      _horizontalGroup.percentWidth = 100; 
      _horizontalGroup.percentHeight = 100; 

      //EXPLICITLY SETTING THEM WORKS, BUT IS STATIC :-(
      //_horizontalGroup.width = 200; 
      //_horizontalGroup.height = 200; 
      addChild(_horizontalGroup); 
     } 
    } 
} 

消費MXML代碼

<?xml version="1.0" encoding="utf-8"?> 
<s:VGroup id="grpControls" width="200" height="200"> 
    <ReportControls:ReportGridSelector width="100%" height="100%"/> 
</s:VGroup> 

如果我明確地定義_horizontalGroupwidthheight屬性,一切都顯示正常。如果我嘗試_horizontalGroup.percentWidthpercentHeight,所有的控件都會被擠在一起。

有什麼想法是怎麼回事?

+0

此處還有一些我已經找到的相關文章。 [Flex:CreateNewComponent](http://stackoverflow.com/questions/489024/flex-how-to-create-an-entirely-new-component) [Flex Custom Component PercentWidth/Height](http:// stackoverflow。 com/questions/2473085/flex-custom-component-percentage-width-height) [如何獲取Flex組件以使用ActionScript填充可用空間](http://stackoverflow.com/questions/2090845/how-to-get -flex-components-to-fill-available-space-using-actionscript) – DynaWeb

回答

1

當顯示列表從updateDisplayList失效時執行佈局。

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

    _horizontalGroup.width = unscaledWidth; 
    _horizontalGroup.height = unscaledHeight; 
} 

理解Flex組件的生命週期:

http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_2.html

createChildren

創建組件的任何子組件。例如, ComboBox控件包含一個TextInput控件和一個Button控件,作爲 子組件。

有關更多信息,請參閱實施createChildren()方法。

的updateDisplayList

的大小和位置上的所有先前的財產和樣式設置基於 屏幕上的組件的孩子,並提請任何皮膚或組件使用 圖形元素。組件的父容器決定組件的大小。

+0

感謝Jason的魅力!我正在慢慢地從MXML實施的組件轉換,並將重新審視生命週期。 – DynaWeb