在纯Actionscript代码中模拟includeInLayout = false

时间:2011-02-19 13:39:49

标签: flex actionscript layout include simulate

如果您了解Flex,您可能知道属性“includeInLayout”的作用。如果不是,则此属性使组件的父级忽略组件的边界(如宽度和高度)以呈现它们自己的边界。

以下参考说明:

  

指定此组件是否为   包含在父母的布局中   容器。如果为true,则对象为   包含在其父容器中   布局和尺寸和位置   它的父容器按其布局   规则。如果为false,则对象大小和   位置不受其影响   父容器的布局。

     

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/core/UIComponent.html#includeInLayout

在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;
  • 更改scrollRect;
  • 涂抹面膜;

没有成功。

干杯...

3 个答案:

答案 0 :(得分:1)

你试图比较不同的东西。 在第一个示例中,宽度和高度来自UIComponent类,它实际上表示由Flex Framework提供的“测量”值。

在第二种情况下,宽度和高度是Flash Player提供的渲染形状的实际大小。

如果你打开UIComponent的实现,你会发现它实际上将宽度和高度的原始含义隐藏在$ width和$ height中,并为它提供了自己的实现。

所以要回答你的问题,你无法实现你想要直接使用Shapes(纯Flash组件)的工作 每次在图形上下文中绘制内容时,宽度和高度都会相应更新。

您可以选择:

  1. 在一个图形中绘制第一个,在另一个图形中绘制第二个并仅测量第一个形状

  2. 自己计算宽度和高度。这就是容器的实际操作,只是跳过includeInLayout = false子组件。

答案 1 :(得分:0)

如果您查看UIComponent源代码,您会发现,该标志includeInLayout用于失效机制(exatcly in size invalidation)。如果组件的includeInLayout为false,则不重新计算其父级的大小及其大小。 无效是本机flex框架机制,在纯AS项目中不存在。

答案 2 :(得分:0)

您需要覆盖容器中的宽度和高度:

override public function get width() : Number {
    // place your code here
    return 100;
}

override public function get height() : Number {
    // place your code here
    return 100;
}