【发布时间】:2011-09-28 11:12:48
【问题描述】:
我已经阅读了一些教程等,并发现我可以在代码中定义我的所有 UI 组件获得相同的结果。
例如:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("hello");
LinearLayout ll = new LinearLayout(this);
ll.addView(tv);
setContentView(ll);
}
相当于
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
+
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
为此使用 XML 有哪些性能/可维护性优势?
如果程序员更喜欢使用代码而不是 XML,应该考虑哪些额外的因素?
【问题讨论】:
-
对这种重复的问题表示歉意(正如 Yan 指出的那样).. 我确实进行了搜索,但没有找到任何相关内容。
标签: android xml android-layout