---
title: "Sencha第二章 布局（1）Box布局 "
author: "zenoh"
date: "2011-09-26T22:38:54Z"
tags: []
categories: ["移动开发"]
slug: "sencha-1-box"
draft: false
lastmod: "2011-09-26T22:38:54Z"
id: 1181027
url: "https://zenoh.iteye.com/blog/1181027"
---

Sencha Touch里的布局有五种

o [hbox](http://192.168.1.150:8080/output/Ext.layout.HBoxLayout.html)

o [vbox](http://192.168.1.150:8080/output/Ext.layout.VBoxLayout.html)

o [card](http://192.168.1.150:8080/output/Ext.layout.CardLayout.html)

o [fit](http://192.168.1.150:8080/output/Ext.layout.FitLayout.html)

o [auto](http://192.168.1.150:8080/output/Ext.layout.ContainerLayout.html)\[默认]

实际上可以分为Box布局和Fit布局两种。

Sencha touch里的布局应该理解为：该控件内部子项的排列方式。

我们今天先来看看box布局

 

Box布局

　　顾名思义，box布局就是一个个的box组成的。

　　　　hbox: 水平排列、垂直居中、靠左置顶

　　　　vbox: 竖直堆叠、水平居中、靠上置顶

 

**hbox：**

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)hbox

```
Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'icon.png',
    glossOnIcon: false,
    onReady : function() {
        var pnl = new Ext.Panel({
            fullscreen: true,
            layout: 'hbox',
            items:[
                {xtype:'button',text:'按钮1'},
                {xtype:'button',text:'按钮2'},
                {xtype:'button',text:'按钮3'}
            ]
        });
    }
});
```

 ![](http://pic002.cnblogs.com/images/2011/284200/2011082315443046.png)

 

**vbox：**

　　将以上的hbox改为vbox

![](http://pic002.cnblogs.com/images/2011/284200/2011082315444971.png)

 

　　但是，只是知道这些，还不足以玩转box布局，下面让我们看看其他常见的box布局实例。

**vbox变型：**

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)vbox变型

```
Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'icon.png',
    glossOnIcon: false,
    onReady : function() {
        var pnl = new Ext.Panel({
            fullscreen: true,
            layout: 'vbox',
            defaults: {
                flex: 1
            },
            items:[
                {xtype:'button',text:'按钮1'},
                {xtype:'button',text:'按钮2'},
                {xtype:'button',text:'按钮3'}
            ]
        });
    }
});
```

![](http://pic002.cnblogs.com/images/2011/284200/2011082315450755.png)

　　关于这里的flex，sencha Touch使用了css3中的弹性和模型，所以大家如果有兴趣可以看看 扩展阅读：[css3中的弹性盒模型](http://www.jzxue.com/wangyesheji/css/201102/24-6340.html)

 

**vbox变型2：**

　　在上面代码的defaults中加入width : '100%'，得到下图

![](http://pic002.cnblogs.com/images/2011/284200/2011082315452177.jpg)

 

　　了解以上内容之后，我们来想想经典的九宫格布局如何实现吧！

　　相必大家也已经心中有数了。

 

**经典的九宫格布局：**

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)九宫格

```
Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'icon.png',
    glossOnIcon: false,
    onReady : function() {
        var pnl = new Ext.Panel({
            fullscreen: true,
            layout: 'vbox',
            defaults: {
                flex: 1,
                width: '100%',
                defaults: {
                    flex: 1,
                    height: '100%'
                }    
            },
            items:[{
                xtype: 'panel',
                layout: 'hbox',
                items:[
                    {xtype:'button',text:'按钮1'},
                    {xtype:'button',text:'按钮2'},
                    {xtype:'button',text:'按钮3'}
                ]
            },{
                xtype: 'panel',
                layout: 'hbox',
                items:[
                    {xtype:'button',text:'按钮4'},
                    {xtype:'button',text:'按钮5'},
                    {xtype:'button',text:'按钮6'}
                ]
            },{
                xtype: 'panel',
                layout: 'hbox',
                items:[
                    {xtype:'button',text:'按钮7'},
                    {xtype:'button',text:'按钮8'},
                    {xtype:'button',text:'按钮9'}
                ]
            }]
        });
    }
});
```

　![](http://pic002.cnblogs.com/images/2011/284200/2011082315453224.png)

 

　　嫌紧挨着不舒服？别急，我们还有些属性没用上！你没有猜错那就是-----margin、padding！你知道怎么做的！

 

**松散九宫格：**

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)松散九宫格

```
Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'icon.png',
    glossOnIcon: false,
    onReady : function() {
        var pnl = new Ext.Panel({
            fullscreen: true,
            layout: 'vbox',
            defaults: {
                flex: 1,
                width: '100%',
                padding: 10,
                defaults: {
                    flex: 1,
                    height: '100%',
                    margin: 10
                }    
            },
            items:[{
                xtype: 'panel',
                layout: 'hbox',
                items:[
                    {xtype:'button',text:'按钮1'},
                    {xtype:'button',text:'按钮2'},
                    {xtype:'button',text:'按钮3'}
                ]
            },{
                xtype: 'panel',
                layout: 'hbox',
                items:[
                    {xtype:'button',text:'按钮4'},
                    {xtype:'button',text:'按钮5'},
                    {xtype:'button',text:'按钮6'}
                ]
            },{
                xtype: 'panel',
                layout: 'hbox',
                items:[
                    {xtype:'button',text:'按钮7'},
                    {xtype:'button',text:'按钮8'},
                    {xtype:'button',text:'按钮9'}
                ]
            }]
        });
    }
});
```

![](http://pic002.cnblogs.com/images/2011/284200/2011082315454315.png)