3.25.测量并改变容器尺寸

3.25.1 问题
你需要它的根据子组件改变某个容器的尺寸。
3.25.2 解决办法
覆盖容器的measure 属性,当Flex 框架调用updateDisplayList 方法的时候调用该属性。
3.25.3 讨论
无论何时,只要容器需要确定子组件有多大以及根据所有的式样和约束信息自己有多大的时候,Flex 框架都会调用measure 方法来确定容器本身的尺寸。类似3.24 节覆盖updateDisplayList 方法的做法,你可以覆盖measure 方法来执行所有可能需要改变尺寸的自定义计算。
+展开
-XML
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.core.Container;
import mx.core.UIComponent;
override protected function measure():void
{
super.measure();
var childrenWidth:int = 0;
var childrenHeight:int = 0;
//loop through all children, and determine the
height and width
//of all the children components
for(var i:int = 0; i<this.numChildren; i++)
{
var obj:UIComponent = (getChildAt(i) as
UIComponent);
if(obj is Container)
{
//here we are using the viewMetricsAndPadding
//so that we get any style information
affiliated
//with the child as well as its actual width
childrenWidth +=
Container(obj).viewMetricsAndPadding.left+
Container(obj).viewMetricsAndPadding.right+obj.width;
childrenHeight +=
Container(obj).viewMetricsAndPadding.top+
Container(obj).viewMetricsAndPadding.bottom+obj.height;
}e
else
{
childrenWidth += obj.width;
childrenHeight += obj.height;
}
//set this components measured height based on our
calculations
measuredHeight = childrenHeight;
measuredWidth = childrenWidth;
}

]]>
</mx:Script>
</mx:HBox>

下一步,使用ExtendableCanvas.mxml 文件,向这个Canvas 里添加子组件来测试它是否适当的扩大了。
+展开
-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxmlwidth="1400"
height="500xmlns:cookbook="oreilly.cookbook.*">

<mx:Script>
<![CDATA[
import mx.containers.Panel;
private function addChildToExtendableCanvas():void
{
var panel:Panel = new Panel();
panel.height = 100 + Math.random()*200;
panel.width = 100 + Math.random()*200;
extCanvas.addChild(panel);
}

]]>
</mx:Script>
<mx:Button click="addChildToExtendableCanvas()"
label="add child"/>

<cookbook:ExtendableCanvas id="extCanvasy="50"
verticalScrollPolicy="offhorizontalScrollPolicy="off"/>

</mx:Canvas>

当更多的子组件添加到Container 里面时,ExtendableCanvas 实例将使用所有子组件的宽和高加上padding 样式信息指定的值来重绘并调整自己的大小。

加支付宝好友偷能量挖...


评论(0)网络
阅读(130)喜欢(0)flash/flex/fcs/AIR