简体   繁体   English

"为什么使用 XML 创建 GUI 是 Android 中的一个好习惯"

[英]Why using XML to create GUI is a good practice in Android

I am from Java Swing background.我来自 Java Swing 背景。 May I know why using XML to create GUI is a good practice in Android?我可以知道为什么在 Android 中使用 XML 创建 GUI 是一种很好的做法吗? For instance, instead of writing the code in (Which makes me feel more comfortable with it as I use to Swing desktop application)例如,而不是编写代码(这让我在使用 Swing 桌面应用程序时感觉更舒服)

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

Using XML layouts has many advantages over Java code. 与Java代码相比,使用XML布局有许多优点。 You get easy references to strings, drawables, dimensions, themes, etc. But the biggest advantage is automatic support for multiple configurations. 您可以轻松参考字符串,绘图,尺寸,主题等。但最大的优势是自动支持多种配置。 Without changing your code you can have different layouts for landscape and portrait by just having an XML layout in layout-land/ and layout-port/. 在不更改代码的情况下,只需在layout-land /和layout-port /中使用XML布局,就可以为横向和纵向设置不同的布局。 And you can do the same to adapt the layout to different resolutions, languages, etc. 你也可以这样做,使布局适应不同的分辨率,语言等。

If GUI programming through XML is really a good stuff, why it still hasn't become a common practice among GUI desktop application developers? 如果通过XML进行GUI编程确实很好,为什么它仍然没有成为GUI桌面应用程序开发人员的常见做法?

Considering that .NET development alone probably exceeds all other desktop GUI development combined, I would argue that it has "become a common practice among GUI desktop application developers". 考虑到单独的.NET开发可能超过了所有其他桌面GUI开发的组合,我认为它已经“成为GUI桌面应用程序开发人员的常见做法”。

Moreover, using XML for GUIs is one facet of the larger "declarative UI" trend, which has many non-XML forms, such as the JSON-ish QML for Qt. 此外,使用XML进行GUI是更大的“声明性UI”趋势的一个方面,它具有许多非XML形式,例如用于Qt的JSON-ish QML。 AFAICT, all major GUI development is moving to incorporate such declarative markup in one form or fashion. AFAICT,所有主要的GUI开发都在以一种形式或方式合并这种声明性标记。

As i think you are from the Java Swing background so may be you consider the programmatically creating layout easy task. 因为我认为你来自Java Swing背景,所以你可能会考虑以编程方式创建布局简单的任务。 But there are many reasons that you can think of XML strategy: 但是有很多原因可以让你想到XML策略:

1) XML layout design is easy to understand as well keep application user interface seperate from the application code. 1)XML布局设计易于理解,同时保持应用程序用户界面与应用程序代码分离。

2) There ary many devices in the market having diffirent-diffrent display sizes and Android provides an easier task to create UI for all using XML and keeping them seperate. 2)市场上有许多设备具有不同的显示尺寸,Android提供了一个更容易的任务,即为所有使用XML创建UI并保持它们分离。

3) Also if you want to develop an multilingual application then XML is the best one. 3)另外,如果你想开发一个多语言应用程序,那么XML是最好的。

Bottom line: 底线:

Only use the Java code for layout if you really need otherwise you have still best option XML. 如果您确实需要,只使用Java代码进行布局,否则您仍然拥有最佳选项XML。

Think of creating a UI using HTML/CSS. 考虑使用HTML / CSS创建UI。 I always find that handy as compared to Swing. 与Swing相比,我总觉得它很方便。 And I believe, its not just me, but other folks too think in the same way ie Jelly Swing . 我相信,它不仅仅是我,而且其他人也以同样的方式思考,即Jelly Swing

Well, for one thing, it allows you to use GUI builders (like the eclipse-powered builder Google provides). 嗯,首先,它允许您使用GUI构建器(如谷歌提供的eclipse驱动的构建器)。

It also helps you separate your business logic from your display logic. 它还可以帮助您将业务逻辑与显示逻辑分开。

Probably, though, you're just really good at Swing code, and you've forgotten how long it took to build up those skills. 但是,你可能真的擅长Swing代码,而且你已经忘记了构建这些技能需要多长时间。 :) :)

I have to say, either ways can work well if developer is skilled. 我不得不说,如果开发人员熟练,这两种方法都可以很好地运作。 However, I think we have to consider how this work in a team. 但是,我认为我们必须考虑这项工作在团队中的运作方式。 I agree with tangentstorm. 我同意tangentstorm。 In this way, we can easy to decouple UI developer and app developer. 通过这种方式,我们可以轻松地将UI开发人员和应用程序开发人员分离。 In a team, ppl can only focus what they are doing. 在团队中,人们只能关注他们正在做的事情。

Here are my thoughts: 这是我的想法:

  • a nice way to take care of my GUI plumbing. 一个很好的方式来照顾我的GUI管道。
  • whenever I need to experiment with GUI dimensional values, code doesn't need recompilation 每当我需要试验GUI维度值时,代码就不需要重新编译
  • XML Layouts are usually very easy to understand - initial GUI development becomes simplistic XML布局通常很容易理解 - 初始GUI开发变得简单

One thing that you should consider is that performance of creating your UI dynamically<\/strong> in your code has better performance in android<\/strong> .您应该考虑的一件事是,在代码中动态创建 UI<\/strong>的性能在 android 中具有更好的性能<\/strong>。 Telegram<\/em> has so good performance also uses dynamic UI. Telegram<\/em>具有如此出色的性能还使用动态 UI。 Also, you can check article<\/a> about dynamic and XML in performance in android that dynamic always has better performance.此外,您可以查看有关动态和 XML 在 android 中的性能的文章<\/a>,动态总是具有更好的性能。 But it's obvious that dynamic UI<\/strong> is much harder than XML<\/strong> .但很明显,动态 UI<\/strong>比XML<\/strong>更难。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM