注意:组件和控件是有区别的。组件对应的英文是component,控件对应的英文是control;控件是带有界面的,组件则未必有界面;控件属于组件,可以说它是带有界面的组件。比如Button有界面,因此可以说它是控件,也可以说它是组件。LinearLayout没有界面,因此它不能算是控件,但它却是组件。本文中由于涉及了带有和不带有界面的组件,因此,用组件泛指这两者。
有些组件,比如Button,可以在程序中用setWidth和setHeight来设定其大小,这是非常方便的。但有些组件却没有这两个设定大小的方法,比如ImageButton、Spinner以及LinearLayout等等,那么如何在程序中根据需要,动态地设定他们的大小呢?下面就用实际的例子来说明这个问题。

2. 将图片文件magnifier.png拖入到项目的res/drawable-mdpi文件夹下。mangifier.png的内容如下:

3. 在strings.xml中,增加如下粗体字代码。这些代码,将会被Spinner使用:
"spin_prompt">请选择城市
"cities">
- 北京
- 上海
- 南京
- 乌鲁木齐
- 哈尔滨
- 符拉迪沃斯托克
4. 修改main.xml,使之如下:
< xml version="1.0"encoding="utf-8" >
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/magnifier"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/cities"
android:prompt="@string/spin_prompt"
/>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
/>
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
/>
不难看出,mainx.ml有一个Button,一个ImageButton,一个Spinner和两个EditText。