2011-05-27 24 views
0

我陷入了一個我沒有想到的行爲,請解釋我理解並修復它。 - 問題如下:我有一個持有一個標籤的類,這個類在itemRenderer內部使用,整個事情都處理withotu異常,但它不會顯示標籤的文本。 我試着添加一個按鈕,但問題依然存在 - 它從來沒有實際添加按鈕。Flex通過類和itemRenderer彈出意外的行爲

爲什麼?

public class BusTreeNode extends UIComponent 
{ 
    protected var label : Label; 

    public function BusTreeNode() 
    { 
     super(); 

     label = new Label(); 
     this.addChild(label); 
     label.x = 40; 
     label.y = 2; 
     label.text = "test"; 
    } 
} 

...裏面的itemRenderer:

<sl:BusTreeNode width="100%" /> 

回答

2

我想這個問題是你有你的BusTreeNode類不measure()實現。所以你的組件的默認大小是0x0。此聲明:

<sl:BusTreeNode width="100%" /> 

仍然給0作爲高度。

您可以閱讀更多關於Flex組件測量here。而this article對於創建自定義組件非常有用。

+0

超酷。 :)我終於成功展示了這個標籤。從來沒有想過在渲染器中將這些複雜的覆蓋類用於Flex 4.它對我而言仍然有點混亂,但每天都在這裏變得清晰明瞭:D謝謝。 – 2011-05-27 09:48:59

+0

如果您保持代碼簡單,這非常簡單。我建議你從簡單的事情開始,並首次使用MXML渲染器。所以@RIAstar的激情也是有意義的:) – Constantiner 2011-05-27 09:52:01

1

它更容易從火花(或MX Flex 3中)ItemRenderer類擴展。創建一個新的MXML文件是這樣的:

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" > 

    <!-- label --> 
    <s:Label id="labelDisplay" /> 

</s:ItemRenderer> 

「labelDisplay的」的「文本」屬性將自動由list(或其他數據組件)來設置它在使用

此外,在Flex。組件生命週期中,應將可視元素添加到createChildren()方法中的displayList。所以,如果你絕對要它寫在純ActionScript,你能做到這樣,但它很難:

public class MyItemRenderer extends ItemRenderer { 

    private var button:Button; 

    override protected function createChildren():void { 
     super.createChildren(); 

     button = new Button(); 
     addElement(button); 
    } 

    override public function set label(value:String):void { 
     super.label = button.label = value; 
    } 

} 
+0

現在它真的幫助:) +1謝謝。 :) – 2011-05-27 10:56:17