Form容器默认使用FormLayout布局排列子表单项。FormLayout继承自VerticalLayout,它对FormItem容器进行纵向排列。如果你想实现纵向的排列表单,这当然好,但是很多时候往往不是这样。看看下面的例子。
通常情况下,我看到开发者在实现一个表单布局的时候,会放弃FormLayout布局而是使用一堆嵌套的HGroup和VGroup,尽管这种做法有点傻X,效果也不太理想。
还记得Flex 3中的ConstraintRows和ConstraintColumns么?它们在Flex 4中换了个ConstraintLayout的马甲又出现了,这个Layout可以用在Form中。我发现许多Flex开发者不知道这件事,这不奇怪,因为它们在官方文档中的说明少得可怜。而且,如果使用Google搜索它,你只能得到MX组件的相关内容。
上面的表单在Spark Form容器中使用ConstraintLayout替代了FormLayout,这个Layout为FormItem容器提供了类似于网格布局的功能,同时实现跨行、跨列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <?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" viewSourceURL="srcview/index.html"> <s:layout> <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/> </s:layout> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <s:Form> <s:layout> <s:ConstraintLayout> <s:constraintRows> <s:ConstraintRow id="row1" /> <s:ConstraintRow id="row2"/> <s:ConstraintRow id="row3"/> </s:constraintRows> <s:constraintColumns> <s:ConstraintColumn id="col1" width="100"/> <s:ConstraintColumn id="col2" width="100"/> <s:ConstraintColumn id="col3" width="100"/> </s:constraintColumns> </s:ConstraintLayout> </s:layout> <s:FormItem label="First Name" left="col1:3" right="col2:53" top="row1:0" bottom="row1:0" skinClass="com.shinynet.shinylib.skins.CondensedStackedFormItemSkin"> <s:TextInput width="100%"/> </s:FormItem> <s:FormItem label="Last Name" left="col2:53" right="col3:3" top="row1:0" bottom="row1:0" skinClass="com.shinynet.shinylib.skins.CondensedStackedFormItemSkin"> <s:TextInput width="100%"/> </s:FormItem> <s:FormItem label="Address" left="col1:3" right="col3:3" top="row2:0" bottom="row2:0" skinClass="com.shinynet.shinylib.skins.CondensedStackedFormItemSkin"> <s:TextInput width="100%"/> </s:FormItem> <s:FormItem label="City" left="col1:3" right="col1:3" top="row3:0" bottom="row3:0" skinClass="com.shinynet.shinylib.skins.CondensedStackedFormItemSkin"> <s:TextInput width="100%"/> </s:FormItem> <s:FormItem label="State" left="col2:3" right="col2:3" top="row3:0" bottom="row3:0" skinClass="com.shinynet.shinylib.skins.CondensedStackedFormItemSkin"> <s:TextInput width="100%"/> </s:FormItem> <s:FormItem label="Zip" left="col3:3" right="col3:3" top="row3:0" bottom="row3:0" skinClass="com.shinynet.shinylib.skins.CondensedStackedFormItemSkin"> <s:TextInput width="100%"/> </s:FormItem> </s:Form> </s:Application> |
下面的内容和本文主题没什么关系,但我还是要说一下。我使用了自定义的FormItem皮肤CondensedStackedFormItemSkin,这个皮肤移除了sequenceLabelDisplay和helpContentGroup这两个外观部件,因为它们是不必要的,而且占用了太多空间。你可以从ShinyLib库中得到它们,你会发现它非常易用。
转自: http://zengrong.net/post/1572.htm