2010-05-28 56 views
1

當我將visible屬性設置爲false爲容器中的某個子項時,如何獲取容器的大小?在下面的示例中,當點擊「切換」時,「containerB」被隱藏,但主容器的可滾動區域沒有調整大小。 (我不想通過大量的空白區域滾動。)當兒童的能見度發生變化時調整容器大小?

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> 
<mx:Script> 
    <![CDATA[ 
     public function toggle():void { 
      containerB.visible = !containerB.visible; 
     } 
    ]]> 
</mx:Script> 
<mx:VBox height="300" width="200" horizontalAlign="center"> 
    <mx:Button label="Toggle" click="toggle()" width="200"/> 
    <mx:VBox id="containerA" height="400" width="150" horizontalAlign="center"> 
     <mx:Button label="A" height="400" width="100"/> 
    </mx:VBox> 
    <mx:VBox id="containerB" height="400" width="150" horizontalAlign="center"> 
     <mx:Button label="B" height="400" width="100"/>   
    </mx:VBox> 
</mx:VBox> 

回答

2
<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
<mx:Script> 
    <![CDATA[ 
     public function toggle():void { 
      containerB.visible = !containerB.visible; 
     } 
    ]]> 
</mx:Script> 
<mx:VBox height="300" width="200" horizontalAlign="center"> 
    <mx:Button label="Toggle" click="toggle()" width="200"/> 
    <mx:VBox id="containerA" height="400" width="150" horizontalAlign="center"> 
     <mx:Button label="A" height="400" width="100"/> 
    </mx:VBox> 
    <mx:VBox id="containerB" height="400" width="150" horizontalAlign="center" includeInLayout="{containerB.visible}"> 
     <mx:Button label="B" height="400" width="100"/>   
    </mx:VBox> 
</mx:VBox> 
</mx:Application> 

嗨,只是使containerB includeInLayout屬性是依賴於它的可見屬性,

我只是說includeInLayout =「{} containerB.visible」在conatinerB屬性列表,這是工作,我希望這是西隧ü是luking爲

有GR8時間

ANKUR夏爾馬

0

很多方面,我認爲鑑於你目前的代碼,你應該聽戲和containerB隱藏事件。

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="creationComplete()"> 
<mx:Script> 
    <![CDATA[ 
     public function toggle():void { 
      containerB.visible = !containerB.visible; 
     } 
public function creationComplete():void{ 
containerB.addEventListener(FlexEvent.SHOW, onContainerBChange); 
containerB.addEventListener(FlexEvent.HIDE, onContainerBChange); 
} 
public function onContainerBChange():void{ 
if(this.containerB.visible == true){ 
this.mainContainer.width = this.containerB.width + this.containerA.width 
this.mainContainer.height = this.containerB.height + this.containerA.height 
} else { 
this.mainContainer.width = this.containerA.width; 
this.mainCintainer.height = this.containerA.height; 
} 
} 

    ]]> 
</mx:Script> 
<mx:VBox height="300" width="200" horizontalAlign="center" id="mainContainer"> 
    <mx:Button label="Toggle" click="toggle()" width="200"/> 
    <mx:VBox id="containerA" height="400" width="150" horizontalAlign="center"> 
     <mx:Button label="A" height="400" width="100"/> 
    </mx:VBox> 
    <mx:VBox id="containerB" height="400" width="150" horizontalAlign="center"> 
     <mx:Button label="B" height="400" width="100"/>   
    </mx:VBox> 
</mx:VBox> 

我在瀏覽器中編寫代碼,所以這應該被視爲僞代碼。對你來說,如果不是在onContainerBChange處理程序中調整大小代碼,而是使顯示列表失效並將代碼放入updateDisplayList()中,那麼對你來說是一大好處。

作爲一個完全拋開;我希望你的真實代碼不使用VBox,只有一個容器在裏面。在這個簡單的例子中,沒有理由不能完全消除containerA和containerB,只需在VBox中有三個按鈕。

相關問題