Linear, Relative, Constraint, Table, Frame and so on and so forth. Android applications have a whole bunch of layouts to choose from when you want to design your application. The question is, which one is the best?

线性,相对,约束,表格,框架等。 当您要设计应用程序时,Android应用程序具有许多布局可供选择。 问题是,哪个最好?

Before we go into detailing the different layouts, we’ll first go over the view object hierarchy and Android’s drawing process.

在详细介绍不同的布局之前,我们将首先介绍视图对象的层次结构和Android的绘制过程。

视图和视图组 (View and ViewGroup)

Think of ViewGroup as the parent class of any view and also, the base class for layouts. It represents an object which is the container for other views. For example, a LinearLayout is a ViewGroup since it can contain views and other layouts as well.

可以将ViewGroup视为任何视图的父类,也可以将其视为布局的基类。 它代表一个对象,是其他视图的容器。 例如, LinearLayoutViewGroup,因为它也可以包含视图和其他布局。

View, on the other hand, is the basic building block of UI elements. Views can be a part of a ViewGroup. For example, a TextView is a View.

另一方面,视图是UI元素的基本构建块。 视图可以是ViewGroup的一部分。 例如, TextViewView

测量->布局->绘制->重复 (Measure -> Layout -> Draw -> Repeat)

Layouts are saved as XML files in Android. But how do they get converted to the objects we see on the screen? Each XML file gets instantiated (read: inflated) and a view hierarchy tree is formed. This means that if you have layout B that is nested inside layout A, they will have a child — parent relationship (layout A is the parent of layout B). Once the tree is formed, there are 3 phases that will happen: Measure, Layout and Draw. Each of these phases traverses the tree in a Depth First Search order.

布局在Android中另存为XML文件。 但是如何将它们转换为我们在屏幕上看到的对象? 每个XML文件都被实例化(读取:膨胀),并形成视图层次树。 这意味着,如果您将布局B嵌套在布局A内,则它们将具有子级父关系(布局A是布局B的父级)。 树形成后,将分为三个阶段:度量,布局和绘制。 这些阶段中的每个阶段都以“ 深度优先搜索”顺序遍历树。

测量 (Measure)

In the first phase, each parent node figures out certain constraints its children have regarding their size. It passes these limitations downward to its children, where each child will evaluate its own size (how big it wants to be) and take into consideration the limitations it has been given and its children’s limitations.

在第一阶段,每个父节点找出其子节点对其大小的某些约束。 它将这些限制向下传递给其子代,每个子代将评估自己的大小(想要多大),并考虑已赋予的限制及其子代的限制。

布局 (Layout)

Here, each node will decide the final size and position of each of its children on the screen.

在这里,每个节点将决定其每个子级在屏幕上的最终大小和位置。

画 (Draw)

Starting from the root node, which draws itself, it then tells its children to draw themselves. In this fashion, what happens is that a parent will be drawn and its children will be drawn on top of it.

从绘制自身的根节点开始,然后告诉子节点绘制自己。 以这种方式,发生的是将绘制一个父级,并在其顶部绘制其子级。

Keeping the process above in mind, you should try to keep the layout of your application as shallow as possible so as to reduce the time it takes to traverse the view hierarchy

牢记上述过程,您应该尝试使应用程序的布局尽可能浅,以减少遍历视图层次结构所需的时间

布局细目 (Layouts Breakdown)

线性的 (Linear)

Organizes its children in a row with an orientation of vertical or horizontal. Meaning, the views will either be all in one row or one column. You can specify the direction by using the android:orientation attribute.

以垂直或水平方向连续组织其子级。 意思是,视图将全部排成一行或一列。 您可以使用android:orientation属性指定方向。

One interesting feature a Linear Layout has is the layout_weight attribute. This is used to tell Linear Layout how to divide the space between child views. It is useful when you want your layout to be consistent among devices and orientations.

线性布局的一项有趣功能是layout_weight属性。 这用于告诉线性布局如何在子视图之间划分空间。 当您希望布局在设备和方向之间保持一致时,这很有用。

Let’s say you wanted the first TextView, containing the word Hello, to always take up 3/4 of the screen’s width. To do this, we can use the layout_weight attribute.

假设您希望第一个包含单词Hello的 TextView始终占据屏幕宽度的3/4。 为此,我们可以使用layout_weight属性。

相对的 (Relative)

As the name implies, this layout will set its inner child views in relative position. This can keep your layout hierarchy flat with no nested view groups. At the same time, however, each Relative Layout has to undergo a process of two Measure passes, which can impact performance.

顾名思义,此布局会将其内部子视图设置在相对位置。 这样可以使布局层次结构平坦,没有嵌套的视图组。 但是,与此同时,每个相对布局都必须经历两次“测量通过”的过程,这可能会影响性能。

One useful feature of a RelativeLayout is the ability to center a child view by using the centerInParent attribute.

RelativeLayout的一个有用功能是能够通过使用centerInParent属性来使子视图居中

约束 (Constraint)

A constraint is a connection or an alignment to the element the constraint is tied to. You define various constraints for every child view relative to other views present. This gives you the ability to construct complex layouts with a flat view hierarchy (no nested ViewGroups). Similar to RelativeLayout, this layout also requires two Measure passes.

约束是对该约束所绑定的元素的连接或对齐。 您可以相对于当前其他视图为每个子视图定义各种约束。 这使您能够使用平面视图层次结构(无嵌套ViewGroups)构造复杂的布局。 与RelativeLayout相似,此布局还需要两次Measure传递。

帧 (Frame)

This layout is used only to hold a single child view, thus blocking any other view in the layout. The layout itself will be as big as its biggest child view (visible or not), plus some padding.

此布局仅用于保存单个子视图,从而阻止布局中的任何其他视图。 布局本身将与其最大的子视图一样大(可见或不可见),外加一些填充。

Avoid having several child views inside a FrameLayout since it will be difficult to avoid the child views from overlapping one another. You can control the positions of these child views by assigning the layout_gravity attribute to each child.

避免在FrameLayout中包含多个子视图,因为很难避免子视图彼此重叠。 您可以通过将layout_gravity属性分配给每个子视图来控制这些子视图的位置。

列表视图/网格视图 (List View/Grid View)

Use when you have a need to present several items on screen (like in a restaurant menu). List View is a single column list that the user can scroll through. You can think of Grid View as a List View with more than one column.

需要在屏幕上显示多个项目时使用(例如在餐厅菜单中)。 列表视图是用户可以滚动浏览的单列列表。 您可以将Grid View视为具有多个列的List View。

What is important to know about these layouts is that the Views are dynamic and created at runtime. To make the items populate at runtime, you need to use an AdapterView.

有关这些布局的重要信息是视图是动态的,并在运行时创建。 为了使项目在运行时填充,您需要使用AdapterView 。

表格布局 (TableLayout)

Very similar to Grid View, this layout arranges its children into rows and columns. Each layout will contain several TableRow objects, each defining a row.

与网格视图非常相似,此布局将其子级按行和列排列。 每个布局将包含几个TableRow对象,每个对象定义一行。

Don’t be afraid to try different layouts until you find the one that works best for you. Feel free to let me know in the comments below which layout is most useful to you and why.

在找到最适合您的布局之前,不要害怕尝试不同的布局。 请在下面的评论中让我知道哪种布局对您最有用以及为什么。

翻译自: https://www.freecodecamp.org/news/how-to-make-sense-of-the-many-android-layouts-693b262706e0/

如何理解许多Android布局相关推荐

  1. android 布局权重问题(最近布局经常坑爹)

    android 布局 权重 With layout_weight you can specify a size ratio between multiple views. E.g. you have ...

  2. android布局的作用,Android UI布局经验总结

    如何在Android中动态设置颜色透明?10%20%到100% Android布局分析工具HierarchyView Android使用include/merge/ViewStub优化布局 List的 ...

  3. Android布局原理与优化

    Android布局原理与优化 目录: 绘制原理 CPU与GPU Android 图形系统的整体架构 RenderThread 硬件加速和软件绘制 invalidate软件绘制流程 invalidate ...

  4. 深入探索Android布局优化(上)

    前言 成为一名优秀的Android开发,需要一份完备的知识体系,在这里,让我们一起成长为自己所想的那样~. Android的绘制优化其实可以分为两个部分,即布局(UI)优化和卡顿优化,而布局优化的核心 ...

  5. android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义...

    1.在项目的根目录有个配置文件"AndroidManifest.xml",是用来设置Activity的属性的如 <?xml version="1.0" e ...

  6. 必读 | 深入理解 Flutter 的布局约束

    前言 本文最初来自于社区成员 Marcelo Glasberg 在 Medium 发表的文章--"初学者应知应会的 Flutter 高级布局规则 (Flutter: The Advanced ...

  7. android布局的复用

    前言 android布局的复用包括:include,merge和ViewSub.下面分别进行介绍. include的使用 include是最常用的复用标签.通过include的layout顺序即可进行 ...

  8. android listview viewstub,Android布局优化之ViewStub控件

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

  9. 一篇文章搞定《Android布局优化》

    ------<一篇文章搞定Android布局优化> 前言 为什么要进行布局优化? Android绘制原理 双缓冲机制 布局加载原理 布局加载优化的一些方法介绍 AsyncLayoutInf ...

最新文章

  1. Speedata uhf_7.6.3.jar错误硬代码问题
  2. bs架构的系统能连接mysql吗_HTTP、BS架构
  3. [19/03/21-星期四] 异常(Exception) (一)
  4. .NET平台下开源框架
  5. Android开发 - ImageView加载Base64编码的图片
  6. 剑桥教授项目 | 机器学习在推荐系统中的应用
  7. 数据库基础(2):简单查询和连接查询
  8. 网上支付之易宝支付接入规范
  9. 【Tool】ELF 和 AXF 文件分析详解
  10. 3d打印材料有哪几种
  11. android蓝牙传输文件到mysql_安卓上的蓝牙数据传输
  12. strcpy函数实现(C语言)
  13. .htaccess文件
  14. 既生Mahout,何生Spark MLlib ?
  15. 破解分布式数据库全局死锁难题 GBase 8c引领数据库领域变革
  16. 提取Insight-MVT_Annotation_Train 数据集标签xml文件中的信息
  17. HDU 4883 TIANKENG’s restaurant (贪心)
  18. 【Neo4j】第 11 章 :在您的 Web 应用程序中使用 Neo4j
  19. 苹果id可以彻底注销吗_QQ帐号可以注销了!你会注销吗?
  20. 【PSO三维路径规划】基于matlab粒子群算法无人机山地三维路径规划【含Matlab源码 1831期】

热门文章

  1. Java—switch case实现两个数的算术运算
  2. DDL 创建与查询数据库
  3. 使用Thread类来创建线程
  4. 如何添加媒体控件Windows Media Player到工具箱中
  5. 9206-初识html
  6. 数据库的相关概念 1006
  7. 爬虫-14-利用代理爬取数据
  8. web-基础入门-web框架-web服务器-wsgi接口
  9. ffmpeg下载直播流
  10. html和jsp的差异