You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

layout-settings.asciidoc 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. ---
  2. title: Layout Formatting
  3. order: 13
  4. layout: page
  5. ---
  6. [[layout.settings]]
  7. = Layout Formatting
  8. While the formatting of layouts is mainly done with style sheets, just as with
  9. other components, style sheets are not ideal or even possible to use in some
  10. situations. For example, CSS does not allow defining the spacing of table cells,
  11. which is done with the [parameter]#cellspacing# attribute in HTML.
  12. Moreover, as many layout sizes are calculated dynamically in the Client-Side
  13. Engine of Vaadin, some CSS settings can fail altogether.
  14. [[layout.settings.size]]
  15. == Layout Size
  16. The size of a layout component can be specified with the
  17. [methodname]#setWidth()# and [methodname]#setHeight()# methods defined in the
  18. [classname]#Sizeable# interface, just like for any component. It can also be
  19. undefined, in which case the layout shrinks to fit the component(s) inside it.
  20. <<../components/components-features#components.features.sizeable,"Sizing
  21. Components">> gives details on the interface.
  22. [[figure.layout.settings.size.undefined]]
  23. .[classname]#HorizontalLayout# with Undefined vs Defined size
  24. image::img/layout_size_undefined_vs_defined.png[]
  25. Many layout components take 100% width by default, while they have the height
  26. undefined.
  27. The sizes of components inside a layout can also be defined as a percentage of
  28. the space available in the layout, for example with
  29. [methodname]#setWidth("100%");# or with the (most commonly used method)
  30. [methodname]#setFullSize()# that sets 100% size in both directions. If you use a
  31. percentage in a [classname]#HorizontalLayout#, [classname]#VerticalLayout#, or
  32. [classname]#GridLayout#, you will also have to set the component as
  33. __expanding__, as noted below.
  34. [WARNING]
  35. ====
  36. __A layout that contains components with percentual size must have a defined
  37. size__!
  38. If a layout has undefined size and a contained component has, say, 100% size,
  39. the component will try to fill the space given by the layout, while the layout
  40. will shrink to fit the space taken by the component, which is a paradox. This
  41. requirement holds for height and width separately. The debug mode allows
  42. detecting such invalid cases; see
  43. <<../advanced/advanced-debug#advanced.debug.hierarchy,"Inspecting
  44. Component Hierarchy">>.
  45. ====
  46. For example:
  47. [source, java]
  48. ----
  49. // This takes 100% width but has undefined height.
  50. VerticalLayout layout = new VerticalLayout();
  51. // A button that takes all the space available in the layout.
  52. Button button = new Button("100%x100% button");
  53. button.setSizeFull();
  54. layout.addComponent(button);
  55. // We must set the layout to a defined height vertically, in
  56. // this case 100% of its parent layout, which also must
  57. // not have undefined size.
  58. layout.setHeight("100%");
  59. ----
  60. If you have a layout with undefined height, such as [classname]#VerticalLayout#,
  61. in a [classname]#UI#, [classname]#Window#, or [classname]#Panel#, and put enough
  62. content in it so that it extends outside the bottom of the view area, scrollbars
  63. will appear. If you want your application to use all the browser view, nothing
  64. more or less, you should use [methodname]#setFullSize()# for the root layout.
  65. [source, java]
  66. ----
  67. // Create the UI content
  68. VerticalLayout content = new VerticalLayout();
  69. // Use entire view area
  70. content.setSizeFull();
  71. setContent(content);
  72. ----
  73. [[layout.settings.size.expanding]]
  74. == Expanding Components
  75. If you set a [classname]#HorizontalLayout# to a defined size horizontally or a
  76. [classname]#VerticalLayout# vertically, and there is space left over from the
  77. contained components, the extra space is distributed equally between the
  78. component cells. The components are aligned within these cells, according to
  79. their alignment setting, top left by default, as in the example below.
  80. Often, you don't want such empty space, but want one or more components to take
  81. all the leftover space. You need to set such a component to 100% size and use
  82. [methodname]#setExpandRatio()#. If there is just one such expanding component in
  83. the layout, the ratio parameter is irrelevant.
  84. If you set multiple components as expanding, the expand ratio dictates how large
  85. proportion of the available space (overall or excess depending on whether the
  86. components are sized as a percentage or not) each component takes. In the
  87. example below, the buttons have 1:2:3 ratio for the expansion.
  88. [classname]#GridLayout# has corresponding method for both of its directions,
  89. [methodname]#setRowExpandRatio()# and [methodname]#setColumnExpandRatio()#.
  90. Expansion is covered in detail in the documentation of the layout components that
  91. support it. See
  92. <<layout-orderedlayout#layout.orderedlayout,"VerticalLayout
  93. and HorizontalLayout">> and
  94. <<layout-gridlayout#layout.gridlayout,"GridLayout">>
  95. for details on components with relative sizes.
  96. [[layout.settings.alignment]]
  97. == Layout Cell Alignment
  98. ((("Alignment", id="term.alignment", range="startofrange")))
  99. ((("[methodname]#setComponentAlignment()#", id="term.setcomponentalignment", range="startofrange")))
  100. When a component in a layout cell has size (width or height) smaller than the
  101. size of the cell, it will by default be aligned in the top-left corner of the
  102. cell. You can set the alignment with the [methodname]#setComponentAlignment()#
  103. method. The method takes as its parameters the component contained in the cell
  104. to be formatted, and the horizontal and vertical alignment. The component must
  105. have been added to the layout before setting the alignment.
  106. <<figure.layout.settings.alignment>> illustrates the alignment of components
  107. within a [classname]#GridLayout#.
  108. [[figure.layout.settings.alignment]]
  109. .Cell Alignments
  110. image::img/layout_alignment.png[]
  111. Note that __a component with 100% relative size can not be aligned__, as it will
  112. take the entire width or height of the cell, in which case alignment is
  113. meaningless. This should especially be noted with the [classname]#Label#
  114. component, which has 100% default width, and the text alignment __within__ the
  115. component is separate, defined by the CSS [literal]#++text-align++# property.
  116. The easiest way to set alignments is to use the constants defined in the
  117. [classname]#Alignment# class. Let us look how the buttons in the top row of the
  118. above [classname]#GridLayout# are aligned with constants:
  119. [source, java]
  120. ----
  121. // Create a grid layout
  122. GridLayout grid = new GridLayout(3, 3);
  123. grid.setWidth(400, Sizeable.UNITS_PIXELS);
  124. grid.setHeight(200, Sizeable.UNITS_PIXELS);
  125. Button topleft = new Button("Top Left");
  126. grid.addComponent(topleft, 0, 0);
  127. grid.setComponentAlignment(topleft, Alignment.TOP_LEFT);
  128. Button topcenter = new Button("Top Center");
  129. grid.addComponent(topcenter, 1, 0);
  130. grid.setComponentAlignment(topcenter, Alignment.TOP_CENTER);
  131. Button topright = new Button("Top Right");
  132. grid.addComponent(topright, 2, 0);
  133. grid.setComponentAlignment(topright, Alignment.TOP_RIGHT);
  134. ...
  135. ----
  136. The following table lists all the [classname]#Alignment# constants by their
  137. respective locations:
  138. .Alignment Constants
  139. |===============
  140. |[parameter]#TOP_LEFT#|[parameter]#TOP_CENTER#|[parameter]#TOP_RIGHT#
  141. |[parameter]#MIDDLE_LEFT#|[parameter]#MIDDLE_CENTER#|[parameter]#MIDDLE_RIGHT#
  142. |[parameter]#BOTTOM_LEFT#|[parameter]#BOTTOM_CENTER#|[parameter]#BOTTOM_RIGHT#
  143. |===============
  144. Another way to specify the alignments is to create an [classname]#Alignment#
  145. object and specify the horizontal and vertical alignment with separate
  146. constants. You can specify either of the directions, in which case the other
  147. alignment direction is not modified, or both with a bitmask operation between
  148. the two directions.
  149. [source, java]
  150. ----
  151. Button middleleft = new Button("Middle Left");
  152. grid.addComponent(middleleft, 0, 1);
  153. grid.setComponentAlignment(middleleft,
  154. new Alignment(Bits.ALIGNMENT_VERTICAL_CENTER |
  155. Bits.ALIGNMENT_LEFT));
  156. Button middlecenter = new Button("Middle Center");
  157. grid.addComponent(middlecenter, 1, 1);
  158. grid.setComponentAlignment(middlecenter,
  159. new Alignment(Bits.ALIGNMENT_VERTICAL_CENTER |
  160. Bits.ALIGNMENT_HORIZONTAL_CENTER));
  161. Button middleright = new Button("Middle Right");
  162. grid.addComponent(middleright, 2, 1);
  163. grid.setComponentAlignment(middleright,
  164. new Alignment(Bits.ALIGNMENT_VERTICAL_CENTER |
  165. Bits.ALIGNMENT_RIGHT));
  166. ----
  167. Obviously, you may combine only one vertical bitmask with one horizontal
  168. bitmask, though you may leave either one out. The following table lists the
  169. available alignment bitmask constants:
  170. .Alignment Bitmasks
  171. |===============
  172. |Horizontal|[parameter]#Bits.ALIGNMENT_LEFT#
  173. |[parameter]#Bits.ALIGNMENT_HORIZONTAL_CENTER#
  174. |[parameter]#Bits.ALIGNMENT_RIGHT#
  175. |Vertical|[parameter]#Bits.ALIGNMENT_TOP#
  176. |[parameter]#Bits.ALIGNMENT_VERTICAL_CENTER#
  177. |[parameter]#Bits.ALIGNMENT_BOTTOM#
  178. |===============
  179. You can determine the current alignment of a component with
  180. [methodname]#getComponentAlignment()#, which returns an [classname]#Alignment#
  181. object. The class provides a number of getter methods for decoding the
  182. alignment, which you can also get as a bitmask value.
  183. [[layout.settings.alignment.size]]
  184. === Size of Aligned Components
  185. You can only align a component that is smaller than its containing cell in the
  186. direction of alignment. If a component has 100% width, as some components have
  187. by default, horizontal alignment does not have any effect. For example,
  188. [classname]#VerticalLayout# is 100% wide by default and can not therefore be horizontally
  189. aligned as such. The problem can sometimes be hard to notice, as the content inside a
  190. [classname]#VerticalLayout# is left-aligned by default.
  191. You usually need to set either a fixed size, undefined size, or less than a 100%
  192. relative size for the component to be aligned - a size that is smaller than the
  193. containing layout has.
  194. If you set the size as undefined and the component itself contains components,
  195. make sure that the contained components also have either undefined or fixed
  196. size.
  197. (((range="endofrange", startref="term.alignment")))
  198. (((range="endofrange", startref="term.setcomponentalignment")))
  199. [[layout.settings.spacing]]
  200. == Layout Cell Spacing
  201. The [classname]#VerticalLayout#, [classname]#HorizontalLayout#, and
  202. [classname]#GridLayout# layouts offer a [methodname]#setSpacing()# method to
  203. enable or disable spacing between the cells of the layout.
  204. For example:
  205. [source, java]
  206. ----
  207. VerticalLayout layout = new VerticalLayout();
  208. layout.setSpacing(false);
  209. layout.addComponent(new Button("Component 1"));
  210. layout.addComponent(new Button("Component 2"));
  211. layout.addComponent(new Button("Component 3"));
  212. ----
  213. The effect of spacing in [classname]#VerticalLayout# and
  214. [classname]#HorizontalLayout# is illustrated in <<figure.layout.spacing>>.
  215. [[figure.layout.spacing]]
  216. .Layout Spacings
  217. image::img/layout_spacing.png[]
  218. The exact amount of spacing is defined in CSS. If the default is not suitable,
  219. you can customize it in a custom theme.
  220. In the Valo theme, you can specify the spacing with the
  221. $v-layout-spacing-vertical and $v-layout-spacing-horizontal parameters, as
  222. described in
  223. <<../themes/themes-valo#themes.valo.variables,"Common
  224. Settings">>. The spacing defaults to the $v-unit-size measure.
  225. When adjusting spacing in other themes, you should note that it is implemented
  226. in a bit different ways in different layouts. In the ordered layouts, it is done
  227. with spacer elements, while in the [classname]#GridLayout# it has special
  228. handling. Please see the sections on the individual components for more details.
  229. [[layout.settings.margins]]
  230. == Layout Margins
  231. Most layout components (with the exception of [classname]#VerticalLayout#) do
  232. not have any margin around them by default. The ordered layouts, as well as
  233. [classname]#GridLayout#, support enabling or disabling a margin with
  234. [methodname]#setMargin()#. This enables CSS classes for each margin in the
  235. HTML element of the layout component.
  236. In the Valo theme, the margin sizes default to $v-unit-size. You can customize
  237. them with $v-layout-margin-top, right, bottom, and left. See
  238. <<../themes/themes-valo#themes.valo.variables,"Common
  239. Settings">> for a description of the parameters.
  240. To customize the default margins in other themes, you can define each margin
  241. with the [literal]#++padding++# property in CSS. You may want to have a custom
  242. CSS class for the layout component to enable specific customization of the
  243. margins, as is done in the following with the [literal]#++mymargins++# class:
  244. [subs="normal"]
  245. ----
  246. .**mymargins**.v-margin-left {padding-left: **10**px;}
  247. .**mymargins**.v-margin-right {padding-right: **20**px;}
  248. .**mymargins**.v-margin-top {padding-top: **40**px;}
  249. .**mymargins**.v-margin-bottom {padding-bottom: **80**px;}
  250. ----
  251. You can enable only specific margins by passing a [classname]#MarginInfo# to the
  252. [methodname]#setMargin()#. The margins are specified in clockwise order for top,
  253. right, bottom, and left margin. The following would enable the left and right
  254. margins:
  255. [source, java]
  256. ----
  257. layout.setMargin(new MarginInfo(false, true, false, true));
  258. ----
  259. The resulting margins are shown in <<figure.layout.margin>> below. The two ways
  260. produce identical margins.
  261. [[figure.layout.margin]]
  262. .Layout Margins
  263. image::img/layout_margin.png[]