将 Flex 组件放置在屏幕中间

发布于 2024-12-03 08:05:39 字数 1740 浏览 1 评论 0原文

我有一个非常普遍的问题,并准备了一个简单的测试用例。

使用 BasicLayout(即绝对定位)时 - 将 Flex 组件放置在应用程序中心的最佳方法是什么?

在下面的测试用例中,我使用的是 x="{width/2}",但这至少给我带来了 2 个问题:

  1. 如何考虑组件尺寸(进度条中的进度条)测试用例)?

  2. 使用绑定{width/2}是个好主意吗?在某些情况下,它不会发送不必要的 DATA_CHANGE 事件吗?

最后我想知道,这一切如何适用于使用 StageScaleMode.SHOW_ALL 的全屏应用程序 - 因为目前我的应用程序在全屏模式下与左上角对齐,并且在全屏模式下有一个白色死区对吧。 (这至少与纯 Flash/AS3 行为有所不同,在纯 Flash/AS3 行为中,全屏内容显示在屏幕中央,左右各有 2 个死区)。

<?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"
    width="700" height="525" 
    backgroundColor="#CCFFCC"
    initialize="systemManager.stage.scaleMode=StageScaleMode.SHOW_ALL">

    <fx:Script>
        <![CDATA[
            private function fullScreen(event:MouseEvent):void {
                stage.displayState = 
                    stage.displayState == StageDisplayState.NORMAL ?
                    StageDisplayState.FULL_SCREEN :
                    StageDisplayState.NORMAL;
            }
        ]]>
    </fx:Script>

    <s:states>
        <s:State name="normal" />
        <s:State name="connected" />
    </s:states>

    <s:CheckBox right="10" bottom="10" 
        label="Full screen" 
        click="fullScreen(event)" />

    <mx:ProgressBar indeterminate="true" 
        x="{width/2}" y="{height/2}" 
        label="Connecting..." labelPlacement="center" 
        includeIn="normal" />

</s:Application>            

在此处输入图像描述

I have a very general question and have prepared a simple test case.

When using BasicLayout (i.e. absolute positioning) - what is the best approach to place a Flex component in the center of the application?

In the test case below I'm using x="{width/2}" but this gives me at least 2 problems:

  1. How do I account for the component dimensions (the ProgressBar in the test case)?

  2. Is the binding {width/2} a good idea to use? Wouldn't it send unnecessary DATA_CHANGE events in some cases?

And finally I wonder, how this all applies to full screen applications using StageScaleMode.SHOW_ALL - because currently my application is aligned to the top-left in the full screen mode and there is a white dead zone on the right of it. (Which is at least a change from pure Flash/AS3 behaviour, where the full screen content is displayed in the center of the screen and 2 dead zones on the left and right).

<?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"
    width="700" height="525" 
    backgroundColor="#CCFFCC"
    initialize="systemManager.stage.scaleMode=StageScaleMode.SHOW_ALL">

    <fx:Script>
        <![CDATA[
            private function fullScreen(event:MouseEvent):void {
                stage.displayState = 
                    stage.displayState == StageDisplayState.NORMAL ?
                    StageDisplayState.FULL_SCREEN :
                    StageDisplayState.NORMAL;
            }
        ]]>
    </fx:Script>

    <s:states>
        <s:State name="normal" />
        <s:State name="connected" />
    </s:states>

    <s:CheckBox right="10" bottom="10" 
        label="Full screen" 
        click="fullScreen(event)" />

    <mx:ProgressBar indeterminate="true" 
        x="{width/2}" y="{height/2}" 
        label="Connecting..." labelPlacement="center" 
        includeIn="normal" />

</s:Application>            

enter image description here

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

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

发布评论

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

评论(3

摇划花蜜的午后 2024-12-10 08:05:39

比这更简单:只需使用 horizo​​ntalCenter< /a> 和 verticalCenter 属性。当在 BasicLayout 内部使用时,组件将始终居中。像这样:

<mx:ProgressBar indeterminate="true" 
                horizontalCenter="0" verticalCenter="0"
                label="Connecting..." labelPlacement="center" />

如果您为这些属性指定“0”以外的值,则组件将从中间偏移您指定为值的像素数量。 (例如 horizo​​ntalCenter="50" 会将组件向父组件中心右侧偏移 50 像素)

该空白可能是由于您使用了 systemManager.stage.scaleMode= StageScaleMode.SHOW_ALL。只需删除该行即可。你为什么要这么做?

编辑

刚刚注意到您在应用程序上使用了固定的“宽度”和“高度”:如果您希望应用程序真正填满屏幕,则必须将这些设置为“100%”。这是造成该空白的另一个可能原因。

Way easier than that: just use horizontalCenter and verticalCenter properties of UIComponent. When used inside a BasicLayout the component will always be centered. Like this:

<mx:ProgressBar indeterminate="true" 
                horizontalCenter="0" verticalCenter="0"
                label="Connecting..." labelPlacement="center" />

If you give these properties a value other than '0' the component will be offset from the middle by the amount of pixels you specify as a value. (e.g. horizontalCenter="50" will offset the component 50 pixels to the right of the center of the parent component)

That white space is probably due to your usage of systemManager.stage.scaleMode=StageScaleMode.SHOW_ALL. Just remove that line. Why are you doing that anyway?

edit

Just noticed you're using a fixed 'width' and 'height' on your Application: you'll have to make those '100%' if you want your app to really fill the screen. This is another possible cause for that white space.

水中月 2024-12-10 08:05:39

要在使用 systemManager.stage.scaleMode=StageScaleMode.SHOW_ALL 时使应用程序居中(而不是左/右对齐),请将 systemManager.stage.align 设置为空字符串 - IE:

// in your Application creationComplete handler
systemManager.stage.scaleMode = StageScaleMode.SHOW_ALL;
systemManager.stage.align = "";

To get your application centered (rather than left/right aligned) when using systemManager.stage.scaleMode=StageScaleMode.SHOW_ALL, set systemManager.stage.align to the empty string - i.e.:

// in your Application creationComplete handler
systemManager.stage.scaleMode = StageScaleMode.SHOW_ALL;
systemManager.stage.align = "";
你爱我像她 2024-12-10 08:05:39

我通常做两件事,

我将对象放置在 VGroups/HGroups 内并对齐,我还使用相对宽度,例如百分比。

就像过去使用 HTML 表格一样思考。它的效果非常好,特别是当你的画布大小被调整分配时。

我的所有画布都必须可调整大小,因为我喜欢只为 PC、移动设备等编写一次应用程序,所以我默认将所有内容设置为可缩放。

I typically do 2 things,

I place objects inside VGroups/HGroups with alignment, I also use relative widths eg percentages.

Think like in the old days of using HTML tables. It works very well especially if your canvas is resized allot.

All of my canvases have to be resizeable as I like to only write my apps once, for pc, mobile etc, so I just make everything scalable by default.

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