Flex includeIn(topLevelApplication的状态)

时间:2012-02-26 17:32:24

标签: flex mobile state mxml flex4.6

在Flex移动应用程序中,我的应用程序组件处理群组中的肖像/横向ios / android和手机/平板电脑等状态。在我的视图中,如果主应用程序具有这些特定状态,我想要包含一个按钮。我不希望任何View检查portroit / landscape和stuff再次设置为自己的状态。另一方面,观点状态是其他事物所必需的。那么,只有当topLevelApplication的状态是景观时,我怎么能说在我的视图中包含按钮呢?

2 个答案:

答案 0 :(得分:2)

使用属性includein =“landscape”如果它存在于多个状态,则可以使用逗号分隔列表

答案 1 :(得分:1)

首先在您的应用程序中添加两个状态

<s:states>
    <s:State name="portrait"/>
    <s:State name="landscape"/>
</s:states>

接下来,将以下功能添加到<fx:Script>部分:

<fx:Script>
    <![CDATA[
        import mx.events.ResizeEvent;

        protected function application1_resizeHandler(event:ResizeEvent):void
        {
            if (width>height)
                this.currentState="landscape";
            else this.currentState="portrait";
        }
    ]]>
</fx:Script>

还可以在应用程序调整大小

上调用上述方法
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" resize="application1_resizeHandler(event)">

现在,如果您想要包含或排除某个组件,只需在所需组件上添加 visible includeIn

visible.landscape="false"

includeIn="landscape"

完整代码示例:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       resize="application1_resizeHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:states>
        <s:State name="portrait"/>
        <s:State name="landscape"/>
    </s:states>

    <fx:Script>
        <![CDATA[
            import mx.events.ResizeEvent;

            protected function application1_resizeHandler(event:ResizeEvent):void
            {
                if (width>height)
                    this.currentState="landscape";
                else this.currentState="portrait";
            }
        ]]>
    </fx:Script>
    <s:Button includeIn="landscape" x="58" y="52" label="Landscape"/>
    <s:Button includeIn="portrait" x="58" y="90" label="Portrait"/>

</s:WindowedApplication>