在孩子的能见度发生变化时调整容器大小?

时间:2010-05-28 01:42:23

标签: flex resize scroll hide visible

当我为容器中的子项设置visible属性为false时,如何让容器调整大小?在下面的示例中,当单击“Toggle”时,“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 个答案:

答案 0 :(得分: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属性设为依赖于其可见属性

即可

我刚刚在conatinerB属性列表中添加了includeInLayout =“{containerB.visible}”,这是有效的,我希望这是你想要的

有一个gr8时间

Ankur Sharma

答案 1 :(得分:0)

很多方法,我认为鉴于您当前的代码,您应该听取show并在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处理程序中使用调整大小代码,而是使invalidylist无效并将代码放入updateDisplayList();

,那么这对您来说是个大加分。

完全抛开;我希望你的真实代码不使用只有一个容器的VBox。在这个简单的例子中,没有理由你不能完全消除containerA和containerB,只是在VBox中有三个按钮。