我想将我的按钮始终放在中心以适应任何变化的应用程序宽度和高度。
如何在mxml中实现?
你想把你的按钮放在什么的中心?有时我认为这样的代码最好在 ActionScript 中作为 updateDisplayList() 方法的一部分来完成。但是,如果您想在 MXML 中执行此操作:
<buttonContainer id="container">
<s:Button x="{container.width/2}" y="{container.height/2}" />
</buttonContainer>
从概念上讲,这应该适用于任何容器。一些容器使它更容易一些;比如一个VGroup:
<s:VGroup horizontalAlign="Center" verticalAlign="middle">
<s:Button />
</s:VGroup>
如果你的容器Button
有一个绝对布局(BasicLayout),它就像使用UIcomponent
's Horizo ntalCenter和verticalCenter属性一样简单:
<s:Group width="400" height="300">
<s:Button horizontalCenter="0" verticalCenter="0" />
</s:Group>
BasicLayout
是每个 Spark 容器的默认布局。mx 组件没有单独的布局类,但概念是一样的:默认情况下定位是绝对的。
我找到了将按钮放置在舞台中央以适应任何变化的舞台宽度和高度的确切解决方案
button.x=(stage.stageWidth-button.width)/2;
button.y=(stage.stageHeight-button.height)/2;