Adobe
产品
Acrobat
Creative Cloud
创意套装
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
更多产品
解决方案
数字营销
数字媒体
教育
金融服务业
政府部门
网页体验管理
更多解决方案
学习帮助下载公司
商店
在线商店
批量许可
查找经销商
搜索
 
信息 登录
欢迎,我的支持
我的帐户
注销
为何登录?登录后可以管理您的帐户,访问试用版下载、产品扩展和社区区域等。
Adobe
产品 分类 购买   搜索  
解決方案 公司
学习
登录 注销 我的货物 我的支持
Date Date
Qty:
Subtotal
Checkout
Adobe 开发者中心 / Flex 开发人员中心 / Flex 快速入门 /

Positioning and laying out Flex components

by Adobe

Adobe logo

Created

22 March 2010

页面工具

在 Facebook 上共享
在 Twitter 上共享
在 LinkedIn 上共享
书签
打印

Tags

要求

用户级别

全部

必需产品

  • Flex (Download trial)

Adobe Flex 4 introduces the Spark component set, which includes Spark containers. Spark containers contain a <s:layout> property which specifies the layout scheme for the container. Spark containers support four layout schemes:

  • BasicLayout (default): Use absolute positioning, or layout constraints.
  • HorizontalLayout: Lay out children in a single horizontal row.
  • VerticalLayout: Lay out children in a single vertical column.
  • TileLayout: Lay out children in multiple rows and columns.

Note: Flex 4 also supports MX containers, which use a predefined set of rules to automatically position all child components that you define within them. For example, if you use a Canvas container, or an MX Application or Panel container, with the layout property set to "absolute", you can specify absolute positions for its children, or use constraint-based layout. For some MX containers, there is no Spark equivalent. For the differences between Spark containers and MX containers when positioning components, see About layout with Spark and MX containers in the Adobe Flex 4 Help.

The <s:Application> tag specifies an Application container. If you do not specify a layout for the Application container, Flex uses the Basic Layout, which is absolute positioning. The layout examples in this QuickStart specify VerticalLayout for the Application container.

This article provides examples on how to position your components in a Flex application:

  • Using automatic positioning
  • Using absolute positioning
  • Using constraint-based layout

The Adobe Flash Builder 4 MXML editor provides visual layout controls to help position your Flex components.

Using automatic positioning

Specify HorizontalLayout, VerticalLayout, or TileLayout for automatic positioning of components in Spark containers. Setting the x or y property of their layout components directly or calling the move() method has no effect, or only a temporary effect.

You can control aspects of the layout by specifying layout properties. The following properties are the most common ones. For more information on layout properties, see About Spark layouts in the Adobe Flex 4 Help.

  • paddingTop, paddingBottom, paddingLeft, paddingRight: Specifies the minimum number of pixels from the edge of a container and the edge of a layout element.
  • horizontalAlign and verticalAlign: Specifies how to align the centers of layout elements within a container.
  • gap: Specifies the distance in pixels between layout elements in a container.

This example specifies a VerticalLayout for the Panel containing the Button and Label components. The layout specifies padding and a gap for the layout elements. The layout also centers components in a vertical columnn.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- LayoutAutomatic.mxml --> <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="380" height="150"> <s:Panel title="My Application"> <s:layout> <s:VerticalLayout paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10"/> </s:layout> <s:Label id="myLabel" width="180" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" click="myLabel.text = 'Hello, World!';" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Using absolute positioning

Specify BasicLayout for absolute positioning of components in Spark containers. BasicLayout is the default layout scheme. If you do not specify a layout scheme, the container defaults to BasicLayout.

With absolute positioning, you specify the position of a component by using its x and y properties, or you specify a constraint-based layout; otherwise, Flex places the child at position 0,0 of the parent container. When you specify the x and y coordinates, Flex repositions the controls only when you change the property values.

This example uses the default BasicLayout scheme for the Panel container, which implements absolute positioning. Then, it sets the x and y properties for both the Label and Button controls so that the two controls overlap.

Tip: Absolute positioning is the only way to make Flex controls overlap.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- LayoutAbsolute.mxml --> <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="200" height="150"> <s:layout> <s:VerticalLayout paddingTop="20" horizontalAlign="center"/> </s:layout> <s:Panel title="My Application" width="180" height="130"> <s:Label id="myLabel" x="10" y="30" fontWeight="bold" fontSize="24" /> <s:Button id="myButton" x="60" y="30" label="Click Me!" click="myLabel.text = 'Hello, World!'" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Using constraint-based layout

You can manage child component size and position simultaneously by using a constraint-based layout, where you anchor the sides or center of a component to positions relative to the component's container. You can use constraint-based layouts with containers that use absolute positioning as a layout scheme. For Spark containers, the default BasicLayout scheme specifies absolute positioning.

You can use constraint-based layout to determine the position and size of the immediate children of any container that supports absolute positioning.

You specify the constraints by using the top, bottom, left, right, horizontalCenter, or verticalCenter style properties of the child component.

Anchoring the edges of a component

You can anchor one or more edges of a component at a pixel offset from the corresponding edge of its container. The anchored component edge stays at the same distance from the container edge when the container resizes.

The top, bottom, left, and right style properties specify the distances between the component sides and the corresponding container sides, in pixels.

The Button control in the following example has its bottom and right edges achored 10 pixels away from the edges of the Panel container that it is in.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- LayoutConstraintsBottomRight.mxml --> <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="200" height="150"> <s:layout> <s:VerticalLayout paddingTop="20" horizontalAlign="center"/> </s:layout> <s:Panel title="My Application" width="200" height="130"> <s:Button id="myButton" label="A button" bottom="10" right="10" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Stretching a component

If you anchor both edges in a dimension, such as top and bottom, the component resizes if the container resizes. The Button control in the following example has all four of its edges anchored 10 pixels away from the edges of the Panel container that it is in.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- LayoutConstraintsEdges.mxml --> <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="200" height="150"> <s:layout> <s:VerticalLayout paddingTop="20" horizontalAlign="center"/> </s:layout> <s:Panel title="My Application" width="160" height="100"> <s:Button id="myButton" label="A button" top="10" bottom="10" left="10" right="10"/> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Anchoring the center of a component

You can also anchor a component's horizontal or vertical center (or both) at a pixel offset from the center of the container. The component does not resize in the specified dimension unless you also use percentage-based sizing.

The horizontalCenter and verticalCenter styles specify distance between the component's center point and the container's center, in the specified direction; a negative number moves the component left or up from the center.

The horizontalCenter and verticalCenter styles specify the offset from the center of a container, in pixels, that a control should be placed at. The Button control in the following example has both styles set to 0 (zero) to perfectly center it in the Panel container.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- LayoutConstraintsCenter.mxml --> <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="200" height="150"> <s:layout> <s:VerticalLayout paddingTop="20" horizontalAlign="center"/> </s:layout> <s:Panel title="My Application" width="180" height="100"> <s:Button id="myButton" label="A button" verticalCenter="0" horizontalCenter="0" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

A more complicated example

This following example uses contraint-based layout to center the Label control and to make the Button control stretch to almost the full length of the Panel. Set the top property of the Label control to 10 to constrain it to appear 10 pixels from to the top of the Panel. Set its horizontalCenter property to 10 to perfectly center it in the Panel.

Set the left and right properties on the Button control to 10 to make it stretch to within 10 pixels of either side of the Panel. Set its bottom property to 10 to constrain its bottom edge to 10 pixels from the lower edge of the Panel.

The following diagram demonstrates the effects of the properties you set visually. The visual layout controls are part of the Design View in Flash Builder.

Constraints

Tip: Do not specify a top or bottom style property with a verticalCenter style property; the verticalCenter value overrides the other properties. Similarly, do not specify a left or right style property with a horizontalCenter style property.

A size determined by constraint-based layout overrides any explicit or percentage-based size specifications. If you specify left and right style properties, for example, the resulting constraint-based width overrides any width set by a width or percentWidth property.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- LayoutConstraints.mxml --> <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="360" height="200"> <s:layout> <s:VerticalLayout paddingTop="20" horizontalAlign="center"/> </s:layout> <s:Panel title="My Application" width="300" height="130"> <s:Label id="myLabel" fontWeight="bold" fontSize="24" top="10" horizontalCenter="0" /> <s:Button id="myButton" label="Click Me!" click="myLabel.text = 'Hello, World!';" bottom="10" height="22" left="10" right="10" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

For more information

  • Laying out components
  • Using constraints-based layouts
  • The Layout Cookbook

Back to top


This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

产品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • 移动应用程序
  • Photoshop
  • Touch Apps

解决方案

  • 数字营销
  • 数字媒体
  • 网页体验管理

行业

  • 教育
  • 金融服务业
  • 政府部门

帮助

  • 产品帮助中心
  • 订货和退货
  • 下载和安装
  • 我的 Adobe

学习

  • Adobe 开发人员连接
  • Adobe TV
  • 培训和认证
  • 论坛
  • 设计中心

购买方式

  • 在线商店
  • 批量许可
  • 查找经销商

下载

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

公司

  • 新闻编辑室
  • 合作伙伴计划
  • 公司社会责任
  • 工作机会
  • 投资者关系
  • 事件
  • 法律
  • 安全
  • 联系 Adobe
选择您的地区 中国(更改)
选择您的地区 关闭

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

使用条款 | 隐私政策和 Cookies (更新)

京 ICP 备 10217899 号 京公网安备 110105010404