如何将垂直布局中的标签移动到flex 4.5中的任意位置?

发布于 2024-11-15 18:08:40 字数 317 浏览 2 评论 0原文

我知道这对于 Flex 专家来说非常简单,但我才刚刚开始。

例如我有这个:

<s:Label id="lbl2"
         alpha="0.0"
         text="Cute Software Engineer"
         color="#ffffff"
         fontSize="32" />

我想将我的标签移动到右上角?

我无法将布局更改为基本/绝对布局,因为我希望标签始终位于屏幕中央。几个动画之后,我想将标签移动到右上角。

I know this is very simple for those expert in flex but im just starting out.

For instance i have this:

<s:Label id="lbl2"
         alpha="0.0"
         text="Cute Software Engineer"
         color="#ffffff"
         fontSize="32" />

I'd like to move my label say to the upper right?

I can't change my layout to a basic/absolute layout since i want my labels centered in the screen all the time. After a few animations, i'd like to move the label to the upper right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-11-22 18:08:40

这里有几个选项(像往常一样!)。显而易见的答案是放弃垂直布局并重构为绝对布局,按照 Harry 的建议使用约束以及 Horizo​​ntalCenter 和 VerticalCenter 属性,然后无论何时/无论何时都可以放置元素。

如果您绝对需要保持垂直布局,另一个选择(更复杂)是创建一个包装器(如组)来包装垂直布局的组件。然后从 VerticalLayout 中删除该元素,设置其位置,然后将其添加到包装器中。

我创建了一个小型测试项目来说明第二个选项。单击位于底部的按钮将从垂直布局中删除标签并将其添加到位于右上角的绝对包装器中...您还可以单击“替换”按钮将元素添加回垂直组件...希望这会有所帮助!

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            import mx.core.IVisualElement;

            protected function remove_clickHandler(event:MouseEvent):void
            {
                // remove element from relative container and store an instance of it
                var tempElement:IVisualElement = relativeContainer.removeElement(lbl2);

                // set the elements new position
                tempElement.right = 0;
                tempElement.top = 0;

                // Add element to absolute wrapper
                wrapper.addElement(tempElement);

                replace.enabled = true;
                remove.enabled = false;
            }

            protected function replace_clickHandler(event:MouseEvent):void
            {
                // remove element from absolute container and store an instance of it
                var tempElement:IVisualElement = wrapper.removeElement(lbl2);

                // Add element to relative container (at its origial position)
                relativeContainer.addElementAt(tempElement, 1);

                replace.enabled = false;
                remove.enabled = true;
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
    </fx:Declarations>

    <!-- ABSOLUTE WRAPPER CONTAINER (THIS COULD ALSO JUST BE THE APPLICATION ITSELF, AS LONG AS ITS LAYOUT IS SET TO BASIC) -->
    <s:Group id="wrapper" width="800" height="600">

        <!-- FILL TO POINT OUT THAT THIS IS THE ABSOLUTE CONTAINER -->
        <s:Rect id="outerFill" left="0" top="0"
                bottom="0" right="0">
            <s:fill>
                <s:SolidColor color="#FF0000" alpha=".33"/>
            </s:fill>
        </s:Rect>

        <!-- RELATIVE CONTAINER -->
        <s:BorderContainer id="relativeContainer" width="400" height="400"
                  verticalCenter="0" horizontalCenter="0">

            <s:layout>
                <s:VerticalLayout />
            </s:layout>

            <s:Label id="lbl1"
                     text="Cute Software Engineer 1"
                     fontSize="32" />
            <s:Label id="lbl2"
                     text="Cute Software Engineer 2"
                     fontSize="32" />
            <s:Label id="lbl3"
                     text="Cute Software Engineer 3"
                     fontSize="32" />
        </s:BorderContainer>

        <!-- CONTROLS -->
        <s:HGroup bottom="5" right="5">

            <s:Button id="remove" 
                      label="Remove Element"
                      click="remove_clickHandler(event)"/>
            <s:Button id="replace" bottom="0" right="0"
                      label="Replace Element"
                      click="replace_clickHandler(event)"
                      enabled="false"/>
        </s:HGroup>
    </s:Group>

</s:Application>

There are a couple of options here(as usual!). The obvious answer would be to ditch the vertical layout and refactor for an absolute layout, using constraints and the horizontalCenter and verticalCenter properties as Harry suggested, then position your element however/whenever you want.

If you absolutely need to keep the vertical layout, another option(more convoluted) would be to create a wrapper (like a group) to wrap your vertically laid out component. Then remove the element from the verticalLayout, set it's positioning and then add it to the wrapper.

I've created a small test project to illustrate the second option. Clicking the buttons located at the bottom removes a label from the vertical layout and adds it to the absolute wrapper positioned top right...you can also click the Replace button to add the element back to the vertical component... HOPE THIS HELPS!

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            import mx.core.IVisualElement;

            protected function remove_clickHandler(event:MouseEvent):void
            {
                // remove element from relative container and store an instance of it
                var tempElement:IVisualElement = relativeContainer.removeElement(lbl2);

                // set the elements new position
                tempElement.right = 0;
                tempElement.top = 0;

                // Add element to absolute wrapper
                wrapper.addElement(tempElement);

                replace.enabled = true;
                remove.enabled = false;
            }

            protected function replace_clickHandler(event:MouseEvent):void
            {
                // remove element from absolute container and store an instance of it
                var tempElement:IVisualElement = wrapper.removeElement(lbl2);

                // Add element to relative container (at its origial position)
                relativeContainer.addElementAt(tempElement, 1);

                replace.enabled = false;
                remove.enabled = true;
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
    </fx:Declarations>

    <!-- ABSOLUTE WRAPPER CONTAINER (THIS COULD ALSO JUST BE THE APPLICATION ITSELF, AS LONG AS ITS LAYOUT IS SET TO BASIC) -->
    <s:Group id="wrapper" width="800" height="600">

        <!-- FILL TO POINT OUT THAT THIS IS THE ABSOLUTE CONTAINER -->
        <s:Rect id="outerFill" left="0" top="0"
                bottom="0" right="0">
            <s:fill>
                <s:SolidColor color="#FF0000" alpha=".33"/>
            </s:fill>
        </s:Rect>

        <!-- RELATIVE CONTAINER -->
        <s:BorderContainer id="relativeContainer" width="400" height="400"
                  verticalCenter="0" horizontalCenter="0">

            <s:layout>
                <s:VerticalLayout />
            </s:layout>

            <s:Label id="lbl1"
                     text="Cute Software Engineer 1"
                     fontSize="32" />
            <s:Label id="lbl2"
                     text="Cute Software Engineer 2"
                     fontSize="32" />
            <s:Label id="lbl3"
                     text="Cute Software Engineer 3"
                     fontSize="32" />
        </s:BorderContainer>

        <!-- CONTROLS -->
        <s:HGroup bottom="5" right="5">

            <s:Button id="remove" 
                      label="Remove Element"
                      click="remove_clickHandler(event)"/>
            <s:Button id="replace" bottom="0" right="0"
                      label="Replace Element"
                      click="replace_clickHandler(event)"
                      enabled="false"/>
        </s:HGroup>
    </s:Group>

</s:Application>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文