zz962

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

摘译自 http://developer.android.com/resources/tutorials/hello-world.htm

前提
1、安装SDK
2、创建AVD

创建新的Android工程

1、在Eclipse中,选择 File > New > Project....
2、选择“Android Project”


3、填写以下信息,点击Finish

Project name HelloAndroid
Build Target 平台的版本要等于或者小于你AVD的平台版本
Application name Hello, Android
Package name com.example.helloandroid (or your own private namespace)
Create Activity HelloAndroid


各项说明

Project Name Eclipse的工程名,含有project文件的目录名称
Build Target 编译程序所用的SDK版本,要等于或者低于AVD的target版本。
比如,2.1编译的程序可以运行在2.3.3的AVD上,反之则不成。
Application Name 程序名称,将出现在安卓设备上
Package Name package的namespace (遵循Java规则) that you want all your source code to reside under. This also sets the package name under which the stub Activity is generated.

Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.
Create Activity This is the name for the class stub that is generated by the plugin. This is a subclass of Android's Activity class.
Activity是一个可以运行和做事的类。
它可以创建UI
Activity通常被用于作为一个程序的基础。
Min SDK Version This value specifies the minimum API Level on which your application will run. The Min SDK Version should be the same as the Build Target you chose. For example, if the Build Target is Android 2.1, then the Min SDK Version should be 7 or lower (it can never be higher than 7). For more information, see Android API Levels.

src下的HelloAndroid.java如下:

类HelloAndroid派生自Activity,Activity是一个用于执行操作的entity。
一个Application可以拥有多个不同的activity,但是用户在一个时刻只和它们中的一个交互。
方法onCreate在你的Activity启动时由Android系统调用,你应该在这个地方执行所有的初始化和UI setup。一个activity并不要求有UI,但是经常有。

Construct the UI

Take a look at the revised code below and then make the same changes to your HelloAndroid class. The bold items are lines that have been added.

以下代码中行5、12、13、14是新增加的,把它们加入你自己的程序


Tip: An easy way to add import packages to your project is to press Ctrl-Shift-O (Cmd-Shift-O, on Mac). This is an Eclipse shortcut that identifies missing packages based on your code and adds them for you. You may have to expand the import statements in your code for this to work.

Tip:在工程中import package的快捷方式是按Ctrl Shift O。(没试成功)


An Android user interface is composed of hierarchies of objects called Views. A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView.

An Android user interface is composed of hierarchies of objects called Views. 一个View是一个可绘制对象,是你的UI layout中的一个元素。比如按钮,图片或者文本标签。这些对象都是View的子类,其中处理文字的是TextView。

In this change, you create a TextView with the class constructor, which accepts an Android Context instance as its parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView.

在上述修改中,在类的构造函数中创建了一个TextView,它以Android的Context为参数。A Context is a handle to the system;它提供了服务,诸如resolving resources,访问数据库或者参数。类Activity是Context的一个子类,因为你的类是Activity的子类,所以它也是一个Context。所以你可以把this作为Context传给TextView。

Next, you define the text content with setText().

接下来,使用setText定义文字内容。

Finally, you pass the TextView to setContentView() in order to display it as the content for the Activity UI. If your Activity doesn't call this method, then no UI is present and the system will display a blank screen.

最后,把TextView传给setContentView,把它作为Activity UI的content来显示。如果你的Activity没有调用这个方法,那么没有UI元素在屏幕上显示,系统会显示一个空白屏幕。

There it is — "Hello, World" in Android! The next step, of course, is to see it running.

Run the Application

The Eclipse plugin makes it easy to run your applications:

  1. Select Run > Run.
  2. Select "Android Application".

The Eclipse plugin automatically creates a new run configuration for your project and then launches the Android Emulator. Depending on your environment, the Android emulator might take several minutes to boot fully, so please be patient. When the emulator is booted, the Eclipse plugin installs your application and launches the default Activity. You should now see something like this:

Eclipse插件自动为你的工程生成新的运行配置,然后启动Android模拟器。视你的环境而定,Android模拟器的完全启动需要花一点时间,所以请耐心。当模拟器启动之后,Eclipse插件安装你的程序并启动缺省的Activity,现在你应该看到下面的画面。



The "Hello, Android" you see in the grey bar is actually the application title. The Eclipse plugin creates this automatically (the string is defined in theres/values/strings.xml file and referenced by your AndroidManifest.xml file). The text below the title is the actual text that you have created in the TextView object.

灰色bar上的是程序的标题。Eclipse插件自动创建它(这个字符串定义在res/values/strings.xml里,你的AndroidManifest.xml引用了它)。标题下方的文字是TextView中的。

That concludes the basic "Hello World" tutorial, but you should continue reading for some more valuable information about developing Android applications.

Upgrade the UI to an XML Layout

The "Hello, World" example you just completed uses what is called a "programmatic" UI layout. This means that you constructed and built your application's UI directly in source code. If you've done much UI programming, you're probably familiar with how brittle that approach can sometimes be: small changes in layout can result in big source-code headaches. It's also easy to forget to properly connect Views together, which can result in errors in your layout and wasted time debugging your code.

刚才这个例子使用的是一种被称为可编程的UI布局。这意味着你直接在代码里构造程序的UI。如果你做了很多UI编程,你会对这种方式的繁琐深有同感。

That's why Android provides an alternate UI construction model: XML-based layout files. The easiest way to explain this concept is to show an example. Here's an XML layout file that is identical in behavior to the programmatically-constructed example:

那就是为什么Android提供了另一种UI构建模型:基于XML的layout文件。下面这个例子的效果和刚才的一致:


The general structure of an Android XML layout file is simple: it's a tree of XML elements, wherein each node is the name of a View class (this example, however, is just one View element). You can use the name of any class that extends View as an element in your XML layouts, including custom View classes you define in your own code. This structure makes it easy to quickly build up UIs, using a more simple structure and syntax than you would use in a programmatic layout. This model is inspired by the web development model, wherein you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data.

Android XML layout文件的通常结构非常简单:它是一个XML元素的树。每个node是一个View类的名称。你可以在XML layout中使用任意从View派生的类的名字作为元素,包括自定义的。较之编程,这种方式易于快速构建UI。这种模型受Web开发模型启发,这种方式下,程序的外观和逻辑是分离的。

In the above XML example, there's just one View element: the TextView, which has five XML attributes. Here's a summary of what they mean:

上面的XML例子里,只有一个View元素和五个XML属性,下面是它们含义的总结:

AttributeMeaning
xmlns:android

This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute

这个是XML命名空间声明,告诉Android工具,你要参考Android 命名空间中定义的common属性。.

android:id

This attribute assigns a unique identifier to the TextView element. You can use the assigned ID to reference this View from your source code or from other XML resource declarations

这个属性给TextView元素赋予了唯一的标识符。你可以在代码或者其他XML资源声明中引用这个ID。.

android:layout_width

This attribute defines how much of the available width on the screen this View should consume. In this case, it's the only View so you want it to take up the entire screen, which is what a value of "fill_parent" means

这个属性定义了View的可用的宽度。在本例中,由于是唯一的View,所以是fill_parent.

android:layout_height

This is just like android:layout_width, except that it refers to available screen height

和前者类似,这是高度.

android:text

This sets the text that the TextView should display. In this example, you use a string resource instead of a hard-coded string value. The hello string is defined in the res/values/strings.xml file. This is the recommended practice for inserting strings to your application, because it makes the localization of your application to other languages graceful, without need to hard-code changes to the layout file. For more information, see Resources and Internationalization

这个属性设置了TextView要显示的文本。在本例中,你使用字符串资源,而不是硬编码。hello字符串定义在res/values/strings.xml里。这是在程序中添加字符串的推荐方法,因为这样有助于多语言。.


These XML layout files belong in the res/layout/ directory of your project. The "res" is short for "resources" and the directory contains all the non-code assets that your application requires. In addition to layout files, resources also include assets such as images, sounds, and localized strings.

这些XML layout文件位于你工程的res/layout/目录。

The Eclipse plugin automatically creates one of these layout files for you: main.xml. In the "Hello World" application you just completed, this file was ignored and you created a layout programmatically. This was meant to teach you more about the Android framework, but you should almost always define your layout in an XML file instead of in your code. The following procedures will instruct you how to change your existing application to use an XML layout.

Eclipse插件自动为你产生这些layout文件:main.xml。在刚才这个程序里,这个文件被忽略了,你用编程的方式创建了一个layout。这样做主要是为了讲解有关Android framwork,但是你应该始终使用在XML里定义layout的方式而不是使用代码。下面的过程将指导你把现在的代码改变成使用XML layout。

  1. In the Eclipse Package Explorer, expand the /res/layout/ folder and open main.xml(once opened, you might need to click the "main.xml" tab at the bottom of the window to see the XML source). Replace the contents with the following XML:


Save the file.

posted on 2011-10-12 10:23  zz962  阅读(307)  评论(0编辑  收藏  举报