如果您了解 Flex,您可能知道属性“includeInLayout”的作用。如果不是,则此属性使组件的父级在呈现自己的边界时忽略组件的边界(如宽度和高度)。
参考说明如下:
指定此组件是否包含在父容器的布局中。如果为 true,则该对象包含在其父容器的布局中,并由其父容器根据其布局规则调整大小和定位。如果为 false,则对象大小和位置不受其父容器布局的影响。
例如,在 Flex 中:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="application1_creationCompleteHandler(event)">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler( event:FlexEvent ):void
{
trace( container.width, container.height ); // output: 200 200
}
]]>
</mx:Script>
<mx:Canvas id="container">
<mx:Button label="Test"
width="100"
height="100" />
<mx:Button label="Test2"
width="200"
height="200" />
</mx:Canvas>
</mx:Application>
现在,如果我在第二个按钮中设置 includeInLayout="false":
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="application1_creationCompleteHandler(event)">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler( event:FlexEvent ):void
{
trace( container.width, container.height ); // output: 100 100
}
]]>
</mx:Script>
<mx:Canvas id="container">
<mx:Button label="Test"
width="100"
height="100" />
<mx:Button label="Test2"
width="200"
height="200"
includeInLayout="false" />
</mx:Canvas>
</mx:Application>
我知道实现这个属性所涉及的所有框架架构,并且知道这个属性是来自 Flex Framework 的一个属性。我想要的是纯动作脚本中的这种行为。例如:
import flash.display.Shape;
var myBox:Shape = new Shape();
myBox.graphics.beginFill(0xFF0000);
myBox.graphics.drawRect(0, 0, 100, 100);
myBox.graphics.endFill();
addChild(myBox);
trace(width, height); // output: 100 100
var myAnotherBox:Shape = new Shape();
myAnotherBox.graphics.beginFill(0xFF00FF, .5);
myAnotherBox.graphics.drawRect(0, 0, 200, 200);
myAnotherBox.graphics.endFill();
addChild(myAnotherBox);
trace(width, height); // output: 200 200
在纯 Actionscript 中是否有一些等效的实现可以在“myAnotherBox”上重现此行为?
我已经尝试过:
- 改变变换矩阵;
- 改变变换pixelBounds;
- 改变滚动矩形;
- 敷面膜;
而且没有成功。
干杯...