为了深入理解和应用Android框架
系统的控件以及更加熟练的掌握eclips的开发技巧,所以就决定做一个简单的计算器来了解和介绍一下Android界面
设计的基本技巧。界面的样子如下:
这个界面由一个编辑框和很多按钮组成。他的功能跟Windows下的计算其功能差不多,就是实现+-*/等功能。这个还实现了一个菜单的功能。按一下Exit就可以离开这个应用程序了。
Android框架定义了很多布局(Layout)架构。布局就像容器,里面可以装下很多控件。布局里面还可以套用其他的布局。这样就可以实现界面的多样化以及设计的灵活性。在这个界面中,我们应用了一个LinearLayout的布局,它是垂直向下扩展的。在这个LinearLayout中,我们可以应用一个EditText和一个TableLayout作为他的子控件。在TableLayout布局可以实现以表格的形式来布局空间。而TableRow是用来定义每一行。在TableLayout中,列的宽度是取其中这一列中宽度最大的控件的宽度。这个应用程序正好用到5x5的一个表格,TableLayout将是最好的选择了。
Android界面编程的一个优势是他的布局全部可以用
xml文件来描述,实现了即改即现的方式。ADT又在Eclips里面做了一个Outline的插件。当你打开布局xml文件的时候,Outline就会显示布局的所有控件以及他们的结构。双击其中的控件就会打开空间的属性。你就可以直接编辑属性来轻松实现界面的布局。所改的属性也将立刻在xml文件中显示出来。以下是描述这个简单计算器的界面xml文件给大家做个参考:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff808080"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:gravity="right"
android:editable = "false"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:text="4"
android:layout_width="63sp"
android:layout_height="wrap_content" />
android:text="5"
android:layout_width="63sp"
android:layout_height="wrap_content" />
android:text="6"
android:layout_width="63sp"
android:layout_height="wrap_content" />
android:text="*"
android:layout_width="63sp"
android:layout_height="wrap_content" />
android:text="%"
android:layout_width="63sp"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:text="1"
android:layout_width="63sp"
android:layo