通过小项目学Android教程3

1写在前面

亲们!我又回来了!

在上一篇教程中,我介绍了如下内容:

1)如何搭建Android app的开发环境(就是下载ADT然后解压一下,如果没有安装jdk,那么要下载并安装jdk)。

2)一个Android工程包含哪些文件和文件夹:AndroidManifest.xml是必须的,还有/res/layout底下的xml是用来定义UI的,/src底下是java源文件。

3)Activity和View的关系,在/res/layout中用xml定义的UI是如何显示出来的?

4)如何在模拟器中运行Android app。

在这个教程里,如上次所说,我们将亲自做一个计算器,通过这个例子,我将告诉你开发Android app需要的一些UI知识,我将在下一个教程中介绍计算表达式的算法。

做一个计算器包含两部分:

1)计算器的UI,亲应该对此了然于胸了。

2)计算表达式的方法,就是我输入1+2×3,你给我算出来7的方法。

我们先从设计UI开始吧!

2 设计计算器的UI

下图就是最后成型的计算器的UI,虽然简单丑陋了点,但是基本的功能都有了 。

我们该如何做这个UI了?当然我们需要在/res/layout底下的xml文件里面定义这些文本显示框和按钮,并将它们布局好!

对于任何UI设计,我们都需要对基本UI元素和由基本UI元素组成的组合UI元素进行布局,例如在html中,我们用<table><div>等等标签来布局,在android中进行UI设计也是类似的,android app framework提供了如下基本的UI元素:

1)按钮Button

2)文本显示框TextView

3)图片显示控件ImageView

4)…

同时app framework也提供了如下的用来组合UI元素在一起的容器,即各种Layout:

1)线性布局容器LinearLayout

2)相对布局容器RelativeLayout

3)表格布局容器TableLayout

4)网格布局容器GridLayout

5)…

我们先聊聊几种基本的布局容器:

2.1 LinearLayout

LinearLayout是最简单最基本的布局容器,顾名思义,LinearLayout是直线型的布局容器,方向可以为水平方向或者垂直方向,例如用一个LinearLayout放置三个按钮:

上图左边的方向是水平,右边的方向是垂直,以下显示了水平方向(即左图)例子的xml文件:

<LinearLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3" /></LinearLayout>

[小技巧]大家在学习过程中,不妨建一个Android app的工程,遇到例子的时候,把提供的xml拷贝到layout/res底下的xml文件中,自己切身地看看效果。

在一个LinearLayout里面可以嵌套另外的LinearLayout(或者其他类型的Layout容器),例如为了设计下面的UI:

我们可以用一个水平方向的LinearLayout,里面嵌套一个垂直方向的LinearLayout:

下面是对应的xml文件(不妨把这个xml拷到你的工程里的res/layout/activity_main.xml里面试试看哦)

<LinearLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="?android:attr/listPreferredItemHeight"android:padding="6dip"><ImageViewandroid:id="@+id/icon"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_marginRight="6dip"android:src="@drawable/icon" /><LinearLayoutandroid:orientation="vertical"android:layout_width="0dip"android:layout_weight="1"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1"android:gravity="center_vertical"android:text="My Application" /><TextViewandroid:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1"android:singleLine="true"android:ellipsize="marquee"android:text="Simple application that shows how to use RelativeLayout" /></LinearLayout></LinearLayout>2.2 RelativeLayout

RelativeLayout是相对位置布局容器,就是容器里面的UI元素的位置是

1) 相对于这个容器的上下左右,或者

2) 相对于容器内其他元素的位置

例如为了设计下面的UI:

我们可以使用RelativeLayout,指定:

1)输入框的位置在文本“Email”下面。

2)Login按钮在输入框的下面,并且Login按钮在整个RelativeLayout的最左边。

3)Cancel按钮在Login按钮的右边。

喜欢就该珍惜,珍惜就别放弃。

通过小项目学Android教程3

相关文章:

你感兴趣的文章:

标签云: