为什么使用XML创建图形用户界面是Android的一个很好的做法 [英] Why using XML to create GUI is a good practice in Android

查看:353
本文介绍了为什么使用XML创建图形用户界面是Android的一个很好的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从Java Swing的背景。我可以知道为什么使用XML创建图形用户界面是Android的一个很好的做法? ,而不是写在code的(这让我感觉更舒服,因为我用的Swing桌面应用程序)。例如,

I am from Java Swing background. May I know why using XML to create GUI is a good practice in Android? For instance, instead of writing the code in (Which makes me feel more comfortable with it as I use to Swing desktop application)

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);
   }
}

我们写code以XML的方式。

We write the code in XML way.

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

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

在教程中,它指出,

这种结构使得它很容易   快速建立用户界面,使用更   结构和语法比你简单   在一个纲领性的布局将使用。

This structure makes it very easy to quickly build up UIs, using a more simple structure and syntax than you would use in a programmatic layout.

不过,我真的不买的想法,因为我觉得创建一个单独的XML文件是比较麻烦的。

However, I don't really buy the idea, as I feel that creating a separate XML file is more cumbersome.

谁能给一个现实世界的例子(Android中的意义上的),为什么使用XML来构建GUI比裸露的Java code更为优越?

Can anyone give a real world example (in the sense of Android), why using XML to build GUI is more superior than bare Java code?

如果通过XML GUI编程确实是一个好东西,为什么还没有成为其中的GUI桌面应用程序开发者的普遍做法?

推荐答案

使用XML布局拥有的Java code很多优点。你很容易引用的字符串,可绘制,尺寸,主题等,但最大的好处是多次配置自动支持。如果不改变你的code可以只具有布局的土地/和布局端口/一个XML布局有不同的布局为横向和纵向。你可以做同样的适应的布局,以不同的分辨率,语言文字等。

Using XML layouts has many advantages over Java code. 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/. And you can do the same to adapt the layout to different resolutions, languages, etc.

这篇关于为什么使用XML创建图形用户界面是Android的一个很好的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆