Extjs layout 布局

转载:http://www.cnblogs.com/lipan/archive/2011/12/27/2302719.html  

一般的容器类控件都是通过配置项items来添加子控件,那么如何定位子控件和父控件的布局,某些容器类控件,本身就继承了布局方式,如Ext.container.Viewport本身是

一个border布局,有如Ext.form.Panel、Ext.tab.Panel等

大致有十种布局方式

absolute、accordion、anchor、border、card、tale、vbox、hbox、fit.columne

1.   absolut::子元素相对于父元素绝对定位,其中包含了x、y两个配置项在其中

[Js

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
//absolute
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div1' ,
     width: 400,
     height: 300,
     layout:  'absolute' ,
     items: [{
         title:  '面板1' ,
         xtype:  "panel" ,
         html:  "子元素1" ,
         width: 200,
         height: 100,
         x: 50,
         y: 50
 
     }, {
         title:  '面板2' ,
         xtype:  "panel" ,
         html:  "子元素2" ,
         width: 200,
         height: 100,
         x: 100,
         y: 80
 
     }]
});
二、accordison
有的js插件里面accordion都是一个ui控件,但是Ext是通过布局的方式实现的,我们可以用面板控件作为它的折叠项,并且还可以用js来翻动活动项。
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
//accordion
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div2' ,
     width: 400,
     height: 300,
     layout:  'accordion' ,
     items: [{
         tools: [{ type:  'gear' , handler:  function  () {
             Ext.Msg.alert( '提示' '配置按钮被点击。' );
         }
         }, { type:  'refresh' }],
         title:  '面板1' ,
         xtype:  "panel" ,
         html:  "子元素1"
 
     }, {
         title:  '面板2' ,
         xtype:  "panel" ,
         html:  "子元素2"
     }, {
         id:  'panel3' ,
         title:  '面板3' ,
         xtype:  "panel" ,
         html:  "子元素3"
     }]
});
Ext.create( "Ext.Button" , {
     renderTo:  'div2' ,
     text:  "打开第三页" ,
     handler:  function  () {
         Ext.getCmp( 'panel3' ).expand( true );
     }
});
三、anchor
这种布局是表单默认的布局方式,每一项代表一行,支持用anchor配置各子项的width和height,为是表时百分比示占当前容器的百分比,为数字一般为负数,表示父容器的值减去差值,剩下的就是子项的大小
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
//anchor
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div3' ,
     width: 400,
     height: 300,
     layout:  'anchor' ,
     items: [{
         tools: [{ type:  'gear' , handler:  function  () {
             Ext.Msg.alert( '提示' '配置按钮被点击。' );
         }
         }, { type:  'refresh' }],
         title:  '面板1' ,
         xtype:  "panel" ,
         html:  "子元素1" ,
         anchor:  '80% 20%'
 
     }, {
         title:  '面板2' ,
         xtype:  "panel" ,
         html:  "子元素2" ,
         anchor:  '-50 -200'
     }, {
         title:  '面板3' ,
         xtype:  "panel" ,
         html:  "子元素3" ,
         anchor:  '100% 30%'
     }]
});
四border
边界布局, 这个布局可以定义东南西北四个方向的子元素,还有一个居中的子元素,一般用它来做页面整页布局,所以Ext.container.Viewport默认就支持了这个布局方式。
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
//border
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div4' ,
     width: 400,
     height: 300,
     layout:  'border' ,
     defaults: {
         split:  true ,                  //是否有分割线
         collapsible:  true ,            //是否可以折叠
         bodyStyle:  'padding:15px'
     },
     items: [{
         region:  'north' ,             //子元素的方位:north、west、east、center、south
         title:  '北' ,
         xtype:  "panel" ,
         html:  "子元素1" ,
         height: 70
     }, {
         region:  'west' ,
         title:  '西' ,
         xtype:  "panel" ,
         html:  "子元素2" ,
         width: 100
 
     }, {
         region:  'east' ,
         title:  '东' ,
         xtype:  "panel" ,
         html:  "子元素2" ,
         width: 100
 
     }, {
         region:  'center' ,
         title:  '主体' ,
         xtype:  "panel" ,
         html:  "子元素3"
     }, {
         region:  'south' ,
         title:  '南' ,
         xtype:  "panel" ,
         html:  "子元素4" ,
         height: 70
     }]
});



五 card
card像卡片一样可以在各个子元素中切换,每个子元素都单独占据整个容器,可以通过按钮控制处于活跃状态的子元素
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
//card
var  cardNav =  function  (incr) {
     var  l = Ext.getCmp( 'cardPanel' ).getLayout();
     var  i = l.activeItem.id.split( 'card' )[1];
     var  next = parseInt(i, 10) + incr;
     l.setActiveItem(next);
     Ext.getCmp( 'cardPrev' ).setDisabled(next === 0);
     Ext.getCmp( 'cardNext' ).setDisabled(next === 2);
};
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div5' ,
     width: 400,
     height: 300,
     layout:  'card' ,
     activeItem: 1,                   //默认活动项
     id:  'cardPanel' ,
     items: [{
         id:  'card0' ,
         title:  '面板1' ,
         xtype:  "panel" ,
         html:  "子元素1"
 
     }, {
         id:  'card1' ,
         title:  '面板2' ,
         xtype:  "panel" ,
         html:  "子元素2"
     }, {
         id:  'card2' ,
         title:  '面板3' ,
         xtype:  "panel" ,
         html:  "子元素3"
     }],
     bbar: [ '->' , {
         id:  'cardPrev' ,
         text:  '« 前一页' ,
         handler: Ext.Function.bind(cardNav,  this , [-1])
     }, {
         id:  'cardNext' ,
         text:  '后一页 »' ,
         handler: Ext.Function.bind(cardNav,  this , [1])
     }]
 
});
六\column
列布局,按列划分
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
//column
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div6' ,
     width: 400,
     height: 300,
     layout:  'column' ,
     defaults: {                      //设置没一列的子元素的默认配置
         layout:  'anchor' ,
         defaults: {
             anchor:  '100%'
         }
     },
     items: [{
         columnWidth: 4 / 10,         //设置列的宽度
         items: [{
             title:  '面板1' ,
             border:  false ,
             html:  '子元素1'
         }, {
             title:  '面板2' ,
             border:  false ,
             html:  '子元素2'
         }]
     }, {
         width: 120,
         items: [{
             title:  '面板3' ,
             border:  false ,
             html:  '子元素3'
         }]
     }, {
         columnWidth: .40,
         items: [{
             title:  '面板4' ,
             border:  false ,
             html:  '子元素4'
         }]
     }]
 
});

七 fit
自适应布局,子元素会独占全部空间,一般只有一个子项
1
2
3
4
5
6
7
8
9
10
11
12
13
//fit
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div7' ,
     width: 400,
     height: 300,
     layout:  'fit' ,
     items: [{
         title:  '面板' ,
         html:  '子元素' ,
         border:  false
     }]
});

八、table
表格布局,用表格定位的方式去布局,可以使用rowpan和crolpans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//table
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div8' ,
     width: 400,
     height: 300,
     layout: {
         type:  'table' ,
         columns: 4
     },
     defaults: { frame:  true , width: 70, height: 50 },
     items: [
         { html:  '元素1' , rowspan: 3, height: 150 },
         { html:  '元素2' , rowspan: 2, height: 100 },
         { html:  '元素3'  },
         { html:  '元素4'  },
         { html:  '元素5' , colspan: 2, width: 140 },
         { html:  '元素6'  },
         { html:  '元素7'  },
         { html:  '元素8'  }
     ]
});

九、vbox
这个布局把所有的子元素按照纵向排成一列。’
Js]
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
//vbox
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div9' ,
     width: 400,
     height: 300,
     layout: {
         type:  'vbox' ,
         pack:  'start' ,               //纵向对齐方式 start:从顶部;center:从中部;end:从底部
         align:  'stretchmax'              //对齐方式 center、left、right:居中、左对齐、右对齐;stretch:延伸;stretchmax:以最大的元素为标准延伸
     },
     defaults: {
         xtype:  'button'
     },
     items: [{
         text:  '小按钮' ,
         flex: 1                       //表示当前子元素尺寸所占的均分的份数。
     }, {
         xtype:  'tbspacer' ,           //插入的空填充
         flex: 3
     },
 
     {
         text:  '中按钮' ,
         scale:  'medium'
     }, {
         text:  '大按钮' ,
         width: 120,
         scale:  'large' ,
         flex: 1
     }]
})
十、hbox

跟vbox类似,只不过变成了横向的。

[Js]
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
//hbox
Ext.create( 'Ext.Panel' , {
     title:  '容器面板' ,
     renderTo:  'div10' ,
     width: 400,
     height: 300,
     layout: {
         type:  'hbox' ,
         pack:  'end' ,
         align:  'middle' //对齐方式 top、middle、bottom:顶对齐、居中、底对齐;stretch:延伸;stretchmax:以最大的元素为标准延伸
     },
     defaults: {
         xtype:  'button'
     },
     items: [{
         text:  '小按钮'
     },{
         text:  '中按钮' ,
         scale:  'medium'
     }, {
         text:  '大按钮' ,
         width: 120,
         scale:  'large'
     }]
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值