简体   繁体   English

如何在Adobe Flex中对齐中心元素

[英]How to align center elements in adobe flex

    <mx:HBox left="186" textAlign="center" verticalAlign="middle" backgroundColor="#FF0000" top="155" height="44" width="100%" styleName="biletBar">
        <s:Image buttonMode="true" id="prev" source="@Embed(source='arrowLeft.png')"/>
        <s:Label text="tickets from 1 to 12 of 48"/>
        <s:Image buttonMode="true" id="next" source="@Embed(source='arrowRight.png')"/>
    </mx:HBox>

I can't center those elements, should I put a block from the left and add width to move all this elements to the right? 我无法使这些元素居中,是否应该从左侧放置一个块并添加宽度以将所有这些元素向右移动? or if I put them all in an additional box, but how to center it too?, but is there a better way to do that? 还是将它们都放在一个额外的框中,但是如何也将它们居中?但是有更好的方法吗?

The HBox only allows you to specify a vertical alignment as "middle" as you have done above. HBox只允许您像上面一样将垂直对齐方式指定为“中间”。 It doesn't support centering elements horizontally, it just lays the elements out horizontally from left to right. 它不支持水平居中放置元素,而只是将元素从左到右水平放置。

One simple solution is wrap the HBox in a Canvas so you can center it: 一种简单的解决方案是将HBox包裹在Canvas以便将其居中:

<mx:Canvas width="100%">
    <mx:HBox horizontalCenter="0" verticalCenter="0">
        ...
    </mx:HBox>
</mx:Canvas>

The horizontalCenter property tells the Canvas that it should put the center of the HBox 0 pixels from the center of the Canvas . horizontalCenter属性告诉Canvas ,它应该把中心HBox 0像素从中心Canvas You can offset it by putting in non-zero values (same w/ verticalCenter ). 您可以通过放入非零值(与verticalCenter相同)来抵消它。

Notice that the width is not specified on the HBox , this makes the HBox be only as large as it needs to be to display its contents. 请注意,未在HBox上指定width ,这使得HBox只能显示其内容所需的大小。

Another alternative w/out using an extra container, would be similar to what you suggested in your question. 不使用额外容器的另一种选择与您在问题中建议的类似。 Use some Spacer objects to occupy most of the space in the HBox : 使用一些Spacer对象来占据HBox中的大部分空间:

<mx:HBox width="100%" verticalAlign="middle">
    <mx:Spacer width="100%"/>
    <mx:Image ... />
    <mx:Label ... />
    <mx:Image ... />
    <mx:Spacer width="100%" />
</mx:HBox>

In this scenario you have two Spacer objects that request 100% of the width of the parent box. 在这种情况下,您有两个Spacer对象,它们请求父框宽度的100%。 The other objects will get rendered at their normal sizes. 其他对象将以其正常大小进行渲染。 The layout logic will figure out how much space the non-spacer components need, and then divide up the remaining available space for the spacers. 布局逻辑将计算出非垫片组件需要多少空间,然后将剩余的可用空间分配给垫片。 This may be a slightly better solution, in that it uses one less "container" object. 这可能是一个稍微更好的解决方案,因为它使用了较少的“容器”对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM