今天我们来对ExtJS 4.2 的布局(Layout)进行一次系统的学习。在ExtJS 4.2中,提供了十几种布局,我们可以在api中看到:

在这些布局中,我们常用的有Accordion、Border、Column、Fit、Form等。下面我们来看一下具体的用法。

Auto Layout

Auto Layout 是ExtJS 容器的默认布局,当我们定义一个panel的时候,如果没有指定panel的布局,那么它就会使用Auto Layout来组织子元素。

Auto Layout 的用法

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. title: "Auto Layout",
  3. width: 500,
  4. height: 400,
  5. items: [
  6. { xtype: "panel", title: "第一个子Panel", width: 200, height: 100 },
  7. { xtype: "panel", title: "第二个子Panel", width: 150, height: 100 },
  8. { xtype: "textfield", width: 300, fieldLabel: "请输入用户名" }
  9. ],
  10. renderTo: "container"
  11. });

Auto Layout 本身不包含任何特殊的布局功能,它只是提供了一种调用子元素布局系统的通道。

Anchor Layout

Anchor布局将使组件固定于父容器的某一个位置,使用anchor布局的子组件尺寸相对于容器的尺寸,即父容器容器的大小发生变化时,使用anchor布局的组件会根据规定的规则重新渲染位置和大小。

AnchorLayout布局没有任何的直接配置选项(继承的除外),然而在使用AnchorLayout布局时,其子组件都有一个anchor属性,用来配置此子组件在父容器中所处的位置。

Anchor属性为一组字符串,可以使用百分比或者是-数字来表示。配置字符串使用空格隔开,例如:

  • anchor:'75% 25%',表示宽度为父容器的75%,高度为父容器的25%
  • anchor:'-295 -300',表示组件相对于父容器右边距为295,相对于父容器的底部位300
  • anchor:'-250 10%',混合模式,表示组件党对于如容器右边为250,高度为父容器的10%

Anchor Layout 用法:

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. width: 500,
  3. height: 400,
  4. title: "Anchor布局",
  5. layout: "anchor",
  6. x: 60,
  7. y: 80,
  8. renderTo: "container",
  9. items: [
  10. { xtype: 'panel', title: '75% Width and 25% Height', anchor: '75% 25%' },
  11. { xtype: 'panel', title: 'Offset -300 Width & -200 Height', anchor: '-295 -300' },
  12. { xtype: 'panel', title: 'Mixed Offset and Percent', anchor: '-250 10%' }
  13. ]
  14. });

Absolute Layout

Absolute Layout 继承自 Anchor Layout,并增加了X/Y配置选项对子组件进行定位,Absolute布局的目的是为了扩展布局的属性,使得布局更容易使用。

Absolute Layout 用法:

  1. Ext.create('Ext.form.Panel', {
  2. title: 'Absolute Layout',
  3. width: 400,
  4. height: 275,
  5. layout: 'absolute',
  6. url: 'save-form.php',
  7. defaultType: 'textfield',
  8. items: [
  9. { x: 10, y: 10, xtype: 'label', text: 'Send To:' },
  10. { x: 80, y: 10, name: 'to', anchor: '90%' },
  11. { x: 10, y: 40, xtype: 'label', text: 'Subject:' },
  12. { x: 80, y: 40, name: 'subject', anchor: '90%' },
  13. { x: 0, y: 80, xtype: 'textareafield', name: 'msg', anchor: '100% 100%' }
  14. ],
  15. renderTo: 'container'
  16. });

Column Layout

Column 布局用来创建一个多列的布局格式,列宽度可以使用像素值或百分比。

Column布局支持一个columnWidth属性,在布局过程中,使用columnWidth指定每个面板的宽度的百分比,他们的和加起来为1。

columnWidth和width可混合使用,这个时候系统将减去width占用的宽度,然后再根据百分比计算列的宽度。

另外,如果任何子面板没有指定columnWidth值,那么它将占满剩余的空间。

Column Layout 用法:

  1. // 所有列都是百分比,他们的和加起来为1
  2. Ext.create('Ext.panel.Panel', {
  3. title: 'Column Layout',
  4. width: 350,
  5. height: 250,
  6. layout: 'column',
  7. items: [{
  8. title: 'Column 1',
  9. columnWidth: 0.25
  10. }, {
  11. title: 'Column 2',
  12. columnWidth: 0.55
  13. }, {
  14. title: 'Column 3',
  15. columnWidth: 0.20
  16. }],
  17. renderTo: "container"
  18. });
  19.  
  20. // width和columnWidth混合使用
  21. Ext.create('Ext.Panel', {
  22. title: 'Column Layout - Mixed',
  23. width: 350,
  24. height: 250,
  25. layout: 'column',
  26. items: [{
  27. title: 'Column 1',
  28. width: 120
  29. }, {
  30. title: 'Column 2',
  31. columnWidth: 0.7
  32. }, {
  33. title: 'Column 3',
  34. columnWidth: 0.3
  35. }],
  36. renderTo: "container"
  37. });

Border Layout

Border 布局将界面分为上下左右中五个区域,分别用north、south、west、east、center来表示,它的每个子项用region指定元素的位置。

虽然Border布局看上去比较麻烦,但用起来却非常简单

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. width: 500,
  3. height: 300,
  4. title: 'Border Layout',
  5. layout: 'border',
  6. items: [{
  7. title: 'South Region (可调整大小)',
  8. region: 'south', // 所在的位置
  9. xtype: 'panel',
  10. height: 100,
  11. split: true, // 允许调整大小
  12. margins: '0 5 5 5'
  13. }, {
  14. title: 'West Region (可折叠/展开)',
  15. region: 'west',
  16. xtype: 'panel',
  17. margins: '5 0 0 5',
  18. width: 200,
  19. collapsible: true, // 可折叠/展开
  20. id: 'west-region-container',
  21. layout: 'fit'
  22. }, {
  23. title: 'Center Region (必须)',
  24. region: 'center', // 必须指定中间区域
  25. xtype: 'panel',
  26. layout: 'fit',
  27. margins: '5 5 0 0'
  28. }],
  29. renderTo: "container"
  30. });

Accordion Layout

Accordion Layout 是将其子元素以手风琴的效果显示。

它的子元素必须是panel,或者panel的子类。

Accordion Layout 的使用非常普遍,我们来一个简单的示例:

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. title: "Accordion Layout",
  3. width: 300,
  4. height: 300,
  5. defaults: {
  6. // 应用到所有子panel
  7. bodyStyle: 'padding:15px'
  8. },
  9. layout: {
  10. // 布局配置
  11. type: 'accordion',
  12. titleCollapse: false,
  13. animate: true,
  14. activeOnTop: true
  15. },
  16. items: [{
  17. title: 'Panel 1',
  18. html: 'Panel content!'
  19. }, {
  20. title: 'Panel 2',
  21. html: 'Panel content!'
  22. }, {
  23. title: 'Panel 3',
  24. html: 'Panel content!'
  25. }],
  26. renderTo: "container"
  27. });

Card Layout

Card 布局是一种向导试的布局方式,它在显示的时候,本身是没有上一步、下一步按钮的,但提供了上一步、下一步的操作方法,我们需要在界面中添加导航按钮来配合实际的业务需要。

示例代码如下:

  1. var navigate = function (panel, direction) {
  2. var layout = panel.getLayout();
  3. layout[direction]();
  4. Ext.getCmp('move-prev').setDisabled(!layout.getPrev());
  5. Ext.getCmp('move-next').setDisabled(!layout.getNext());
  6. };
  7.  
  8. var panel = Ext.create('Ext.panel.Panel', {
  9. title: '示例向导,
  10. x: 50,
  11. y: 50,
  12. width: 300,
  13. height: 200,
  14. layout: 'card',
  15. bodyStyle: 'padding:15px',
  16. defaults: {
  17. border: false
  18. },
  19. bbar: [
  20. {
  21. id: 'move-prev',
  22. text: '上一步',
  23. handler: function (btn) {
  24. navigate(btn.up("panel"), "prev");
  25. },
  26. disabled: true
  27. },
  28. '->',
  29. {
  30. id: 'move-next',
  31. text: '下一步',
  32. handler: function (btn) {
  33. navigate(btn.up("panel"), "next");
  34. }
  35. }
  36. ],
  37. items: [{
  38. id: 'card-0',
  39. html: '<p>第一步</p>'
  40. }, {
  41. id: 'card-1',
  42. html: '<p>第二步</p>'
  43. }, {
  44. id: 'card-2',
  45. html: '<p>第三步</p>'
  46. }],
  47. renderTo: "container"
  48. });

Fit Layout

Fit Layout 是很常用的一种布局,在Fit布局中,子元素将自动填满整个父容器。

在Fit 布局下,对其子元素设置宽度是无效的。如果在Fit 布局中放置了多个组件,则只会显示第一个子元素。典型的案例就是当客户要求一个window或panel中放置一个GRID组件,grid组件的大小会随着父容器的大小改变而改变。

Fit Layout 示例代码:

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. title: 'Fit Layout',
  3. x: 30,
  4. y: 30,
  5. width: 300,
  6. height: 150,
  7. layout: 'fit',
  8. items: {
  9. title: '内部Panel',
  10. html: '内部Panel 的内容',
  11. bodyPadding: 20,
  12. border: false
  13. },
  14. renderTo: "container"
  15. });

Form Layout

Form Layout 用来组织表单字段的,Form Layout 下的表单字段会被拉伸到表单的宽度。

示例代码如下

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. width: 350,
  3. height: 240,
  4. title: "FormLayout Panel",
  5. layout: 'form',
  6. bodyPadding: 5,
  7. defaultType: 'textfield',
  8. items: [{
  9. fieldLabel: '姓名',
  10. name: 'name',
  11. allowBlank: false
  12. }, {
  13. fieldLabel: '公司',
  14. name: 'company'
  15. }, {
  16. fieldLabel: 'Email',
  17. name: 'email',
  18. vtype: 'email'
  19. }, {
  20. fieldLabel: '年龄',
  21. name: 'age',
  22. xtype: 'numberfield',
  23. minValue: 0,
  24. maxValue: 100
  25. }],
  26. renderTo: "container"
  27. });

Table Layout

Table Layout 将内容绘制在table标签中,table的列数可以指定,还可以通过设置rowSpan和colSpan来创建复杂的布局。

示例代码如下:

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. title: 'Table Layout',
  3. width: 300,
  4. height: 150,
  5. layout: {
  6. type: 'table',
  7. // 列数
  8. columns: 3
  9. },
  10. defaults: {
  11. bodyStyle: 'padding:20px'
  12. },
  13. items: [{
  14. html: 'Cell A content',
  15. rowspan: 2 //占用两行
  16. }, {
  17. html: 'Cell B content',
  18. colspan: 2 //占用两列
  19. }, {
  20. html: 'Cell C content',
  21. cellCls: 'highlight'
  22. }, {
  23. html: 'Cell D content'
  24. }],
  25. renderTo: "container"
  26. });

Box Layout

Box Layout 是HBox Layout 和 VBox Layout 的父类,一般不会直接用到。

HBox Layout

HBox Layout 将子元素放在同一水平位置,通过align设置子元素的对齐方式,对齐方式有:

  • top : 默认的对其方式,顶部对齐
  • middle : 中间对齐
  • bottom : 底部对齐
  • stretch : 拉伸对齐,所有子元素根据父容器的高度拉伸
  • stretchmax : 拉伸对齐,所有子元素根据子元素中最高的高度拉伸

示例代码如下:

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. width: 500,
  3. height: 300,
  4. title: "HBoxLayout Panel",
  5. layout: {
  6. type: 'hbox',
  7. align: 'stretch'
  8. },
  9. items: [{
  10. xtype: 'panel',
  11. title: 'Inner Panel One',
  12. flex: 2
  13. }, {
  14. xtype: 'panel',
  15. title: 'Inner Panel Two',
  16. flex: 1
  17. }, {
  18. xtype: 'panel',
  19. title: 'Inner Panel Three',
  20. flex: 1
  21. }],
  22. renderTo: "container"
  23. });

VBox Layout

VBox Layout 以垂直的方式组织所有子元素。它的子元素可以通过align属性来设置对齐方式,vbox的对齐方式有:

  • left : 左对齐,默认对其方式
  • center : 中间对齐
  • right : 右对齐
  • stretch : 以父容器的宽度拉伸对齐
  • stretchmax : 以所有子元素中的最大宽度拉伸对齐

示例代码如下:

  1. var panel = Ext.create("Ext.panel.Panel", {
  2. width: 500,
  3. height: 400,
  4. title: "VBoxLayout Panel",
  5. layout: {
  6. type: 'vbox',
  7. align: 'center'
  8. },
  9. items: [{
  10. xtype: 'panel',
  11. title: 'Inner Panel One',
  12. width: 250,
  13. flex: 2
  14. },
  15. {
  16. xtype: 'panel',
  17. title: 'Inner Panel Two',
  18. width: 250,
  19. flex: 4
  20. },
  21. {
  22. xtype: 'panel',
  23. title: 'Inner Panel Three',
  24. width: '50%',
  25. flex: 4
  26. }],
  27. renderTo: "container"
  28. });

 

ExtJS 4.2 教程-08:布局系统详解的更多相关文章

  1. Bootstrap栅格系统详解,响应式布局

    Bootstrap栅格系统详解 栅格系统介绍 Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 栅格系统用于通 ...

  2. ExtJs常用布局--layout详解(含实例)

    序言: 笔者用的ExtJs版本:ext-3.2.0 ExtJs常见的布局方式有:border.form.absolute.column.accordion.table.fit.card.anchor ...

  3. Nmap扫描教程之基础扫描详解

    Nmap扫描教程之基础扫描详解 Nmap扫描基础扫描 当用户对Nmap工具了解后,即可使用该工具实施扫描.通过上一章的介绍,用户可知Nmap工具可以分别对主机.端口.版本.操作系统等实施扫描.但是,在 ...

  4. 七牛云存储Python SDK使用教程 - 上传策略详解

    文 七牛云存储Python SDK使用教程 - 上传策略详解 七牛云存储 python-sdk 七牛云存储教程 jemygraw 2015年01月04日发布 推荐 1 推荐 收藏 2 收藏,2.7k  ...

  5. Android ADB命令教程二——ADB命令详解

    Android ADB命令教程二——ADB命令详解 转载▼ 原文链接:http://www.tbk.ren/article/249.html       我们使用 adb -h 来看看,adb命令里面 ...

  6. 约束布局ConstraintLayout详解

    约束布局ConstraintLayout详解 转 https://www.jianshu.com/p/17ec9bd6ca8a 目录 1.介绍 2.为什么要用ConstraintLayout 3.如何 ...

  7. css 13-CSS3属性:Flex布局图文详解

    13-CSS3属性:Flex布局图文详解 #前言 CSS3中的 flex 属性,在布局方面做了非常大的改进,使得我们对多个元素之间的布局排列变得十分灵活,适应性非常强.其强大的伸缩性和自适应性,在网页 ...

  8. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  9. Android Studio系列教程五--Gradle命令详解与导入第三方包

    Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...

随机推荐

  1. 《Redis设计与实现》学习笔记

    第2章 简单动态字符串(SDS) redis的字符串不是直接用c语言的字符串,而是用了一种称为简单动态字符串(SDS)的抽象类型,并将其作为默认字符串. redis中包含字符串值的键值对在底层都是由S ...

  2. Latex常用整理

    会不断更新添加,以便写论文的时候快速查找. 项目 带编号 \begin{enumerate} \setlength{\itemsep}{0pt} \setlength{\parsep}{0pt} \s ...

  3. net mvc cms

    .NET作品集:linux下的.net mvc cms   cms程序架构 本程序是主要是用于企业网站开发的,也可以做博客程序,程序是从之前上一篇的.net 博客程序改进过来的,主要技术由webfor ...

  4. Codeforces 486E LIS of Sequence

    LIS of Sequence 我们先找出那些肯定不会再LIS里面. 然后我们从前往后扫一次, 当前位置为 i , 看存不存在一个 j 会在lis上并且a[ j ] > a[ i ], 如果满足 ...

  5. elasticsearch query dsl

    1.match / match_phrase / match_phrase_prefix / multi_match[查询] 1.1 match 它会根据所给的字符串,进行分词,然后去找出,包含这些分 ...

  6. opencv 学习资料

    [视觉与图像]OpenCV篇:Python+OpenCV实用教程 Python+OpenCV教程15:直方图

  7. django邮件

    1.邮件变量 (django settings.py) ADMINS = [('JOHN','JOHN@example.com'),('zhang','zhang@example.com')] #设置 ...

  8. Ubuntu美化及配置,常见问题解决方案

    安装符合审美观,并且具有可用性的Ubuntu桌面,需要耗费一些时间与精力不过,相信我,这值得去做,你会享受这中间的过程,以及最后的成果 首先,我推荐安装的软件列表如下,在安装前,需要先执行以下的步骤: ...

  9. SDC信息统计分析系统ETL工具的研究与实现[专业:计算机应用技术]

    SDC信息统计分析系统ETL工具的研究与实现[专业:计算机应用技术] http://www.docin.com/p-265530271.html

  10. BZOJ.1003.[ZJOI2006]物流运输(DP 最短路Dijkstra)

    题目链接 容易看出是个最短路+DP.既然答案和天数有关,那么就令\(f[i]\)表示前\(i\)天最小成本. 这个转移很好想: \(f[i]=\min(f[i],\ f[j]+cost(j+1,i)+ ...