我来自Java Swing背景.我是否知道为什么使用XML创建GUI在Android中是一个很好的做法?例如,而不是编写代码(这使我感觉更舒服,因为我使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
我们用XML方式编写代码.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在教程中,它指出,
使用比在编程布局中使用的更简单的结构和语法,这种结构使得快速构建UI变得非常容易.
但是,我并没有真正购买这个想法,因为我觉得创建一个单独的XML文件更加麻烦.
任何人都可以给出一个真实世界的例子(在Android意义上),为什么使用XML构建GUI比裸Java代码更优越?
如果通过XML进行GUI编程确实很好,为什么它仍然没有成为GUI桌面应用程序开发人员的常见做法?