Android程序的界面控件可以直接通过XML布局文件来配置,当然,也可以直接通过程序来创建相应的控件。在本例中将结合这两种方式来完成界面的布局。广州网站建设
本例的界面由两个按钮和一个画布组成,如图2.18所示。两个按钮下面黑色的部分是画布,将要在这里随机绘制实心圆。两个按钮需要在XML布局文件中来配置,画布则通过代码来创建。广州网站建设
![]() |
| 图2.18 随机绘制实心圆程序的主界面 |
- <?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">
- <!-- 下面的代码定义两个按钮 -->
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Draw Random Circle" android:onClick=" onClick_DrawRandomCircle"/>
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Clear" android:onClick="onClick_Clear"/>
- </LinearLayout>
在上面的代码中使用了<Button>标签来定义按钮,在本书后面的内容还会介绍更多的Android控件。在这里只要知道<Button>标签可以创建按钮即可。广州网站设计




