java 为什么在 Android 中使用 XML 来创建 UI 布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5645468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:04:00  来源:igfitidea点击:

Why is XML used for the creation of UI layouts in Android?

javaandroid

提问by rogerstone

I would like to know why we use XML for the creating user interface layouts in Android. I know that it decouples the business logic from the design but what is the significance of the XML other than that?

我想知道为什么我们使用XML 在 Android 中创建用户界面布局。我知道它将业务逻辑与设计分离,但除此之外 XML 的意义是什么?

Also I would like to know the significance of the auto-generated R.javafilein this. All I know that it is generated according to the changes in the resources and that it helps us to access the widgets and resources through their IDs.

我也想知道自动生成的R.java文件在这方面的意义。我只知道它是根据资源的变化生成的,它帮助我们通过它们的 ID 访问小部件和资源。

It would be great if someone could give a clear idea on these two aspects.

如果有人能对这两个方面给出一个清晰的想法,那就太好了。

回答by rogerstone

Unlike what everyone said about the xml being easy and efficient.Here is what i read from Hello Android from Ed Brunnette which made sense.

与每个人所说的 xml 简单高效不同。这是我从 Ed Brunnette 的 Hello Android 中读到的,这是有道理的。

Android is optimized for mobile devices with limited memory and horsepower, so you may find it strange that it uses XML so pervasively. After all, XML is a verbose, human-readable format not known for its brevity or efficiency, right?

Android 针对内存和功率有限的移动设备进行了优化,因此您可能会觉得奇怪,它如此普遍地使用 XML。毕竟,XML 是一种冗长的、人类可读的格式,以其简洁性或效率而著称,对吧?

Although you see XML when writing your program, the Eclipse plug-in invokes the Android resource compiler, aapt, to preprocess the XML into a compressed binary format.**It is this format, not the original XML text, that is stored on the device.

尽管您在编写程序时会看到 XML,但 Eclipse 插件会调用 Android 资源编译器 aapt 将 XML 预处理为压缩的二进制格式。**存储在设备。

This was the kind of answer that i was looking for.(sorry if my question meant otherwise).

这是我正在寻找的那种答案。(对不起,如果我的问题不是这样)。

The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.

之所以选择 XML,主要是因为它的熟悉度和原生支持它的 IDE 工具的数量。例如,开发人员可以选择 JSON 并仍然将其编译为二进制文件。自动生成的 R.java 文件是 IDE 的助手,因此您可以在想要访问资源时获得自动完成的好处。

回答by Abdullah Jibaly

XML is easy to parse and manipulate programmatically, it's basically a tree structure and most UI creation tools already use it. It really has nothing to do with decoupling business logic because you can define Java code in Android using a Model-View-Controller pattern just as well.

XML 易于以编程方式解析和操作,它基本上是一种树结构,大多数 UI 创建工具已经使用它。它实际上与解耦业务逻辑无关,因为您也可以使用模型-视图-控制器模式在 Android 中定义 Java 代码。

The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource. It also stops you from making stupid mistakes since the compiler will complain if you try to access a resource you haven't defined. If you were using a simple properties file you wouldn't know until runtime that the 'key' you are using is missing.

自动生成的 R.java 文件是 IDE 的助手,因此您可以在想要访问资源时获得自动完成的好处。它还可以防止您犯愚蠢的错误,因为如果您尝试访问尚未定义的资源,编译器会抱怨。如果您使用的是一个简单的属性文件,那么直到运行时您才会知道您正在使用的“密钥”丢失了。

回答by PedroAGSantos

Same as why is silverlight with xml the answer is simple xml give power by integration and scalability. R.java is for indexing having things organized is never bad. Sorry my english

与为什么 Silverlight 与 xml 相同,答案是简单的 xml 通过集成和可扩展性提供力量。R.java 用于索引有组织的东西永远不会坏。对不起我的英语

回答by IanNorton

One possible reason is that you need not have any working java underneath in order to be able to see the visual layout of the interface you are working on. The xml ui element/page is essentially a document that you can parse and display. If this were a source file you would have to either carefully parse it or compile and run it (all of which are more complex than parsing xml)

一个可能的原因是,为了能够看到您正在处理的界面的视觉布局,您不需要在下面有任何可用的 java。xml ui 元素/页面本质上是一个可以解析和显示的文档。如果这是一个源文件,您将不得不仔细解析它或编译并运行它(所有这些都比解析 xml 更复杂)

回答by Vladimir Ivanov

Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. Also, you have already mentioned the decoupling.

Xml 本身对人和机器都具有良好的可读性。此外,它具有可扩展性且易于开发。另外,您已经提到了解耦。

As for R.java - it is just the way of accessing widgets. The solution is rather good, as it is using ints instead of string to use less memory and in the same time provides well readable names for the simplicity of development.

至于 R.java - 它只是访问小部件的方式。该解决方案相当不错,因为它使用整数而不是字符串来使用更少的内存,同时为开发的简单性提供了易读的名称。

回答by Hitesh Sahu

Android Layouts are tree like structures with some enforced rules. XMLs are perfect fit for this purpose.

Android 布局是具有一些强制规则的树状结构。XML 非常适合此目的。

JSON also have tree like structure but they are data-oriented while XML is document-oriented.** :

JSON 也有树状结构,但它们是面向数据的,而 XML 是面向文档的。**:

Meaning XML is based on the idea that documents have structural and other semantic elements that can be described without reference to how such elements should be displayed.

含义 XML 基于这样一种思想,即文档具有结构和其他语义元素,可以在不参考这些元素应如何显示的情况下对其进行描述。

The actual display of such a document may vary, depending on the output medium and style preferences.

此类文档的实际显示可能会有所不同,具体取决于输出介质和样式首选项。

While JSON was designed to represent JavaScript and their prime purpose is data exchange . They are well suited in data-oriented areas because of light weight and simplicity & closer in syntactic form to programming data structures.

虽然 JSON 旨在代表 JavaScript,但它们的主要目的是数据交换。它们非常适合面向数据的领域,因为它们重量轻且简单,并且在语法形式上更接近于编程数据结构。