ExtJS 4 官方指南翻译:容器与布局(Layouts and Containers)

简介: 原文:http://docs.sencha.com/ext-js/4-0/#!/guide/layouts_and_containers 翻译:frank/sp42 转载请保留本页信息 布局系统是 Ext JS 的最强大的部分之一。

原文:http://docs.sencha.com/ext-js/4-0/#!/guide/layouts_and_containers

翻译:frank/sp42 转载请保留本页信息

布局系统是 Ext JS 的最强大的部分之一。它可以处理您的应用程序中的每个组件的大小和定位。本指南介绍了如何进行布局的基础知识。

The layout system is one of the most powerful parts of Ext JS. It handles the sizing and positioning of every Component in your application. This guide covers the basics of how to get started with layouts.

容器(Containers)

Ext JS 程序所谓的 UI, 是由组件 Component 组成的(参见《组件指南》。容器 Container 是一种特殊类型的组件,它可以包含其他组件。一个典型的 Ext JS 的应用程序是由不同的组件嵌套而成。

An Ext JS application UI is made up of Components (See the Components Guide for more on Components. A Container is a special type of Component that can contain other Components. A typical Ext JS application is made up of several layers of nested Components.

最常用的容器是面板 Panel。让我们看看如何一个面板是如何作为容器来包含其他组件的:

The most commonly used Container is Panel. Let's take a look at how being a Container allows a Panel to contain other Components:

Ext.create('Ext.panel.Panel', {
    renderTo: Ext.getBody(),
    width: 400,
    height: 300,
    title: 'Container Panel',
    items: [
        {
            xtype: 'panel',
            title: 'Child Panel 1',
            height: 100,
            width: '75%'
        },
        {
            xtype: 'panel',
            title: 'Child Panel 2',
            height: 100,
            width: '75%'
        }
    ]
});
我们刚刚创建了一个渲染到 document.body 的面板 Panel,并且使用 items 配置项来添加两个子面板,属于面板容器(Container Panel)下的子面板。

We just created a Panel that renders itself to the document body, and we used the items config to add two child Panels to our Container Panel.

布局 Layouts

每个容器都有一个布局管理器专门调整容器其子组件的大小和定位。在本节中,我们将讨论如何为一个容器来配置特定类型的布局,以及布局系统如何做到保持一切的同步。

Every Container has a Layout that manages the sizing and positioning of its child Components. In this section we're going to discuss how to configure a Container to use a specific type of Layout, and how the layout system keeps everything in sync.

布局的用法 Using Layouts

在上面的例子中,我们没有为面板容器(Container Panel)指派的布局。请注意子面板都是一个挨着一个布局的,就像正常 DOM 中的块元素将那样子。这是因为所有容器的默认布局是自动布局(Auto Layout)。自动布局不指定任何特殊的定位或子元素的大小规则。假如我们希望两个子面板并排定位,各子面板的宽度为容器宽度的 50%——我们可以在容器身上的 layout 配置项处指定一个“列布局  Column Layout ”的方式。

In the above example we did not specify a layout for the Container Panel. Notice how the child Panels are laid out one after the other, just as normal block elements would be in the DOM. This happens because the default layout for all Containers is Auto Layout. Auto Layout does not specify any special positioning or sizing rules for child elements. Let's assume, for example, we want our two child Panels to be positioned side by side, and to each take up exactly 50% of the width of the Container - we can use a Column Layout simply by providing a layout config on the Container:

Ext.create('Ext.panel.Panel', {
    renderTo: Ext.getBody(),
    width: 400,
    height: 200,
    title: 'Container Panel',
    layout: 'column',
    items: [
        {
            xtype: 'panel',
            title: 'Child Panel 1',
            height: 100,
            width: '50%'
        },
        {
            xtype: 'panel',
            title: 'Child Panel 2',
            height: 100,
            width: '50%'
        }
    ]
});
Ext JS 的全套布局不但简单易用,而且数量众多,几乎囊括所有类型的布局。请参阅 布局的例子,说不定有什么新的想法来着。

Ext JS comes with a full set of layouts out of the box and can accomodate almost any type of layout you can imagine. See the Layout Examples to get an idea of what's possible.

布局系统是如何工作的 How the layout system works

一个容器的布局指的是初始定位问题和容器所有子项的大小问题。框架内部会调用容器的 doLayout 方法执行布局的工序,为所有子项计算正确的大小尺寸和位置,并且更新 DOM。doLayout 方法是完全递归的,所以容器下的任何子项其 doLayout 方法也会被调用。一直持续到组件层次结构结束为止。你通常不会在程序代码中调用 doLayout(),因为该框架相应为你处理的。

A Container's Layout is responsible for the initial positioning and sizing of all of the Container's children. Internally the framework calls the Container's doLayout method which triggers the Layout to calculate the correct sizes and positions for all of the Container's children and update the DOM. The doLayout method is fully recursive, so any of the Container's children that are also will have their doLayout method called as well. This continues until the bottom of the Component hierarchy is reached. You typically will not have to ever call doLayout() in your application code since the framework should handle it for you.

当容器大小变化时,就需要重新进行布局,或添加或删除子组件项目时候,也需要重新布局。通常,我们可以依赖于框架来为我们处理布局的更新,但有时我们要防止框架自动布局,以便我们可以批量地处理多个操作,最后才手动地执行布局的动作。通常我们的某些操作,就是需要这样,具体说,就是在触发布局之前(例如添加或删除子项),先对容器 suspendLayout 标记,就可以防止它执行布局了。而当我们完成所有我们要做的事情后,打开刚关闭的 suspendLayout 标志然后手动执行布局,也就是通过调用容器的 doLayout 方法:

A re-layout is triggered when the Container is resized, or when child Component items are added or removed. Normally we can just rely on the framework to handle updating the layout for us, but sometimes we want to prevent the framework from automatically laying out so we can batch multiple operations together and then manually trigger a layout when we're done. To do this we use the suspendLayout flag on the Container to prevent it from laying out while we perform our operations that would normally trigger a layout (adding or removing items for example). When we're done all we have to do is turn the suspendLayout flag off and manually trigger a layout by calling the Container's doLayout method:

var containerPanel = Ext.create('Ext.panel.Panel', {
    renderTo: Ext.getBody(),
    width: 400,
    height: 200,
    title: 'Container Panel',
    layout: 'column',
    suspendLayout: true // 默认自身会产生一些布局逻辑 Suspend automatic layouts while we do several different things that could trigger a layout on their own
});
// 加入一子项。也可以传入一数组的方式添加 Add a couple of child items.  We could add these both at the same time by passing an array to add(),
// 但假设我们因为某些原因而要逐个加。but lets pretend we needed to add them separately for some reason.
containerPanel.add({
    xtype: 'panel',
    title: 'Child Panel 1',
    height: 100,
    width: '50%'
});
containerPanel.add({
    xtype: 'panel',
    title: 'Child Panel 2',
    height: 100,
    width: '50%'
});
// 关闭标记 Turn the suspendLayout flag off.
containerPanel.suspendLayout = false;
// 执行布局 Trigger a layout.
containerPanel.doLayout();

组件布局 Component Layout

就像一个容器的布局去定义容器的大小和及其个组件项的定位那样,组件 Component 也有一布局调整其内部的子项的大小和位置。组件布局通过 componentLayou t配置项来定义布局。一般来说,你不再需要使用这种配置,除非你正在编写一个自定义的组件,因为所提供的组件其内部元素需要通过自己的布局管理器来调整大小和定位问题。于是大多数组件都使用自动布局(Auto Layout),但更复杂些的组件则需要一个自定义组件的布局(例如面板的头部  header、脚部 footer 和工具栏 toolbar)。

Just like a Container's Layout defines how a Container sizes and positions its Component items, a Component also has a Layout which defines how it sizes and positions its internal child items. Component layouts are configured using the componentLayout config option. Generally, you will not need to use this configuration unless you are writing a custom Component since all of the provided Components which need their internal elements sized and positioned come with their own layout managers. Most Components use Auto Layout, but more complex Components will require a custom component layout (for example a Panel that has a header, footer, and toolbars).

目录
相关文章
|
编解码 移动开发 前端开发
【Bootstrap】<前端框架>Bootstrap布局容器&栅格网格系统
【1月更文挑战第17天】【Bootstrap】<前端框架>Bootstrap布局容器&栅格网格系统
|
8月前
|
Java 虚拟化 容器
(Java)Java里JFrame窗体的基本操作(容器布局篇-1)
容器 容器,我的理解是可以包容其他东西的玩意。它可以是一个盒子,可以是一个虚拟化的物品,可只要能包裹住其他存在质体的东西,那么都可以称作是容器。例如:JPanel组件和JScollPane组件两者都是容器也是组件。 既然有容器,那么容器中的布局就必不可少了。不然不规矩的摆放物品,人类看不习惯,我也看不习惯 ???? 本篇内容,将说明java JFrame窗体里容器中几类布局。 说明:所有在JFrame窗体里的容器布局都会使用setLayout()方法,采用的布局参数都将放进这个方法里 绝对布局 调用窗体容器
236 1
|
前端开发 API 开发者
harmonyOS基础- 快速弄懂HarmonyOS ArkTs基础组件、布局容器(前端视角篇)
本文由黑臂麒麟(6年前端经验)撰写,介绍ArkTS开发中的常用基础组件与布局组件。基础组件包括Text、Image、Button等,支持样式设置如字体颜色、大小和加粗等,并可通过Resource资源引用统一管理样式。布局组件涵盖Column、Row、List、Grid和Tabs等,支持灵活的主轴与交叉轴对齐方式、分割线设置及滚动事件监听。同时,Tabs组件可实现自定义样式与页签切换功能。内容结合代码示例,适合初学者快速上手ArkTS开发。参考华为开发者联盟官网基础课程。
1364 75
harmonyOS基础- 快速弄懂HarmonyOS ArkTs基础组件、布局容器(前端视角篇)
|
设计模式 开发者 UED
123. [HarmonyOS NEXT 实战案例一:SideBarContainer] 侧边栏容器实战:新闻阅读应用侧边栏布局 基础篇
在现代移动应用和平板应用中,侧边栏导航已经成为一种常见且实用的UI设计模式。HarmonyOS NEXT提供了专门的`SideBarContainer`组件来实现这一功能,它能够轻松创建可显示和隐藏的侧边栏布局,非常适合新闻阅读、电子商务、文件管理等应用场景。
490 3
123. [HarmonyOS NEXT 实战案例一:SideBarContainer] 侧边栏容器实战:新闻阅读应用侧边栏布局 基础篇
|
UED 容器
124.[HarmonyOS NEXT 实战案例一:SideBarContainer] 侧边栏容器实战:新闻阅读应用侧边栏布局 进阶篇
在基础篇中,我们学习了如何使用HarmonyOS NEXT的`SideBarContainer`组件创建新闻阅读应用的基本侧边栏布局。本篇教程将深入探讨如何为新闻阅读应用添加更多交互功能和状态管理,提升用户体验。
213 1
124.[HarmonyOS NEXT 实战案例一:SideBarContainer] 侧边栏容器实战:新闻阅读应用侧边栏布局 进阶篇
|
UED 容器
10.HarmonyOS Next布局进阶:嵌套Flex容器与空间分配策略
在HarmonyOS Next的ArkUI框架中,Flex布局是构建用户界面的核心技术之一。通过嵌套使用Flex容器,我们可以创建复杂而灵活的界面结构,满足各种应用场景的需求。本教程将深入探讨如何在HarmonyOS Next中使用嵌套Flex容器实现复杂布局,以及如何合理分配和控制空间。
378 0
|
存储 PyTorch 算法框架/工具
Pytorch学习笔记(4):模型创建(Module)、模型容器(Containers)、AlexNet构建
Pytorch学习笔记(4):模型创建(Module)、模型容器(Containers)、AlexNet构建
650 0
Pytorch学习笔记(4):模型创建(Module)、模型容器(Containers)、AlexNet构建
|
容器
『PyQt5-Qt Designer篇』| 08 Qt Designer中容器布局和绝对布局的使用
『PyQt5-Qt Designer篇』| 08 Qt Designer中容器布局和绝对布局的使用
350 0
教你快速上手Flex弹性盒布局(容器属性)(二)
教你快速上手Flex弹性盒布局(容器属性)
|
容器 iOS开发 Linux
震惊!Uno Platform 响应式 UI 构建秘籍大公开!从布局容器到自适应设计,带你轻松打造跨平台完美界面
【8月更文挑战第31天】Uno Platform 是一款强大的跨平台应用开发框架,支持 Web、桌面(Windows、macOS、Linux)及移动(iOS、Android)等平台,仅需单一代码库。本文分享了四个构建响应式用户界面的最佳实践:利用布局容器(如 Grid)适配不同屏幕尺寸;采用自适应布局调整 UI;使用媒体查询定制样式;遵循响应式设计原则确保 UI 元素自适应调整。通过这些方法,开发者可以为用户提供一致且优秀的多设备体验。
708 0