Android (界面编程#7-可视化编程)

 布局XML文件使用:     
      就如跨平台UI界面库一样,Android也是使用XML文件来存贮界元素持布局,现在流行的一些界面组件都是采用此方式。
在Android中,res/layout资源目录下,会有一个或多个.xml文件,这就是一个界面的布局文件。我们打开一个来看看。我打开当前工程目录下的res/layout/main.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:id="@+id/mainview"
    />

</LinearLayout>

这个文件很简单的布局是指用了一个LinearLayout来布局,里面只有一个TextView界面元素,也就是一个View.当Activity加载View时,就可以在onCreate中直接加载。this.setContentView(R.layout.main);其中R.layout.main就是一个素引值,是由android开发环境编译生成的,是映射到res/layout/main.xml的。
所以setContentView(R.layout.main);等价于,按装main.xml的布局来配置一个layout.然后加载,
与如下代码效果一致
LinearLayout layout = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText(R.string.hello);
layout.addView(tv);
this.setContentView(layout);
其中R.string.hello也是一个资源映射的ID,是指加载res/values/string.xml中的hello对应的值。
 上述例子是一个简单的例子,知道了如何加载布局那如何生成,配置这样的layout文件呢?

可视化编程:
创建一个项目,步聚就如Hello,World例子中一样,
然后找到左边的res/layout/main.xml双击打开,Eclipse右边会 自动打开这个xml文件的layout视图,视图周围有一些操作这个layout的工具栏,拖拉一些你想要的
控件到这个视图上,那么xml文件就会被改变,在最右边的 Outline视图中,有这个布局文件中界面元素的树状结构图。
玩这个东东,想必都会玩,与可视化的VS,Delphi,Swing,QT,没有任何区别,那我们再来一个小例子吧:
posted @ 2009-07-23 20:41  岁月无声  阅读(2245)  评论(0编辑  收藏  举报