现在的位置: 首页 > 综合 > 正文

ExtJS4学习笔记(二)—HBox的使用

2012年12月24日 ⁄ 综合 ⁄ 共 2590字 ⁄ 字号 评论关闭

要使用HBox布局方式,首先的熟悉下一下几个主要属性:

一、align:字符类型,指示组件在容器内的对齐方式。有如下几种属性。

    1、top(默认):排列于容器顶端。

    2、middle:垂直居中排列于容器中。

    3、stretch:垂直排列且拉伸义填补容器高度

    4、stretchmax:垂直拉伸,并且组件以最高高度的组件为准。

二、flex:数字类型,指示组件在容器中水平呈现方式,通俗的讲,就是指示组件在容器中的相对宽度。

三、pack : 字符类型,指示组件在容器的位置,有如下几种属性。

    1、start(默认):组件在容器左边

    2、center:组件在容器中间

    3、end:组件在容器的右边

实例代码:

一、HTML代码:

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>HBox实例--MHZG.NET</title>
  6. <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
  7. <script type="text/javascript" src="../../bootstrap.js"></script>
  8. <script type="text/javascript" src="../../locale/ext-lang-zh_CN.js"></script>
  9. <script type="text/javascript" src="hbox.js"></script>
  10. </head>
  11. <body>
  12. </body>
  13. <div id="d1"></div>
  14. <div id="d2"></div>
  15. <div id="d3"></div>
  16. </html>

hbox.js代码:

 
  1. Ext.onReady(function(){
  2.    var d1 = Ext.create('Ext.Panel',{
  3.   title:'HBox 顶对齐,且组件在容器的左边',
  4.   frame:true,
  5.   width:600,
  6.   height:100,
  7.   items:[{
  8.     anchor:'100%',
  9.     layout: {
  10.         type:'hbox',
  11.         padding:'10',
  12.         pack:'start',
  13.         align:'top'
  14.     },
  15.     defaults:{margins:'0 5 0 0'},
  16.     items:[{
  17.         xtype:'button',
  18.         text: 'Button 1'
  19.     },{
  20.         xtype:'button',
  21.         text: 'Button 2'
  22.     },{
  23.         xtype:'button',
  24.         text: 'Button 3'
  25.     },{
  26.         xtype:'button',
  27.         text: 'Button 4'
  28.     }]
  29.   }]
  30. })
  31.    
  32. d1.render('d1');
  33. var d2 = Ext.create('Ext.Panel',{
  34.   title:'HBox 垂直对齐,且组件在容器的右边',
  35.   frame:true,
  36.   width:600,
  37.   height:100,
  38.   items:[{
  39.     anchor:'100%',
  40.     layout: {
  41.         type:'hbox',
  42.         padding:'10',
  43.         align:'middle',
  44.         pack:'end'
  45.     },
  46.     defaults:{margins:'0 5 0 0'},
  47.     items:[{
  48.         xtype:'button',
  49.         text: 'Button 1'
  50.     },{
  51.         xtype:'button',
  52.         text: 'Button 2'
  53.     },{
  54.         xtype:'button',
  55.         text: 'Button 3'
  56.     },{
  57.         xtype:'button',
  58.         text: 'Button 4'
  59.     }]
  60.   }]
  61. })
  62.    
  63. d2.render('d2');
  64. var d3 = Ext.create('Ext.Panel',{
  65.   title:'HBox 垂直水平居中,并且所有控件高度为最高控件的高度',
  66.   frame:true,
  67.   width:600,
  68.   height:100,
  69.   items:[{
  70.      anchor:'100%',
  71.         
  72.      layout: {
  73.         type: 'hbox',
  74.         padding:'5',
  75.         align:'stretchmax',
  76.         pack:'center'
  77.     },
  78.     defaults:{margins:'0 5 0 0'},
  79.     items:[{
  80.         xtype:'button',
  81.         text: 'Small Size'
  82.     },{
  83.         xtype:'button',
  84.         scale: 'medium',
  85.         text: 'Medium Size'
  86.     },{
  87.         xtype:'button',
  88.         scale: 'large',
  89.         text: 'Large Size'
  90.     }]
  91.   }]
  92. })
  93.    
  94. d3.render('d3');
  95.  })

最后是效果图:

 

另外多种的布局方式,大家自己尝试就OK了。

抱歉!评论已关闭.