【译】Tutorials ---- Hello, World
摘譯自?http://developer.android.com/resources/tutorials/hello-world.htm
前提
1、安裝SDK
2、創(chuàng)建AVD
創(chuàng)建新的Android工程
1、在Eclipse中,選擇?File > New > Project....
2、選擇“Android Project”
3、填寫以下信息,點(diǎn)擊Finish
| Project name | HelloAndroid |
| Build Target | 平臺(tái)的版本要等于或者小于你AVD的平臺(tái)版本 |
| Application name | Hello, Android |
| Package name | com.example.helloandroid (or your own private namespace) |
| Create Activity | HelloAndroid |
各項(xiàng)說明
| Project Name | Eclipse的工程名,含有project文件的目錄名稱 |
| Build Target | 編譯程序所用的SDK版本,要等于或者低于AVD的target版本。 比如,2.1編譯的程序可以運(yùn)行在2.3.3的AVD上,反之則不成。 |
| Application Name | 程序名稱,將出現(xiàn)在安卓設(shè)備上 |
| Package Name | package的namespace (遵循Java規(guī)則) 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是一個(gè)可以運(yùn)行和做事的類。 它可以創(chuàng)建UI Activity通常被用于作為一個(gè)程序的基礎(chǔ)。 |
| 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如下:
package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }類HelloAndroid派生自Activity,Activity是一個(gè)用于執(zhí)行操作的entity。
一個(gè)Application可以擁有多個(gè)不同的activity,但是用戶在一個(gè)時(shí)刻只和它們中的一個(gè)交互。
方法onCreate在你的Activity啟動(dòng)時(shí)由Android系統(tǒng)調(diào)用,你應(yīng)該在這個(gè)地方執(zhí)行所有的初始化和UI setup。一個(gè)activity并不要求有UI,但是經(jīng)常有。
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是新增加的,把它們加入你自己的程序
package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } }
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. 一個(gè)View是一個(gè)可繪制對(duì)象,是你的UI layout中的一個(gè)元素。比如按鈕,圖片或者文本標(biāo)簽。這些對(duì)象都是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.
在上述修改中,在類的構(gòu)造函數(shù)中創(chuàng)建了一個(gè)TextView,它以Android的Context為參數(shù)。A Context is a handle to the system;它提供了服務(wù),諸如resolving resources,訪問數(shù)據(jù)庫或者參數(shù)。類Activity是Context的一個(gè)子類,因?yàn)槟愕念愂茿ctivity的子類,所以它也是一個(gè)Context。所以你可以把this作為Context傳給TextView。
Next, you define the text content with?setText().
接下來,使用setText定義文字內(nèi)容。
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沒有調(diào)用這個(gè)方法,那么沒有UI元素在屏幕上顯示,系統(tǒng)會(huì)顯示一個(gè)空白屏幕。
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:
To learn more about creating and editing run configurations in Eclipse, refer to?Developing In Eclipse, with ADT.
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插件自動(dòng)為你的工程生成新的運(yùn)行配置,然后啟動(dòng)Android模擬器。視你的環(huán)境而定,Android模擬器的完全啟動(dòng)需要花一點(diǎn)時(shí)間,所以請(qǐng)耐心。當(dāng)模擬器啟動(dòng)之后,Eclipse插件安裝你的程序并啟動(dòng)缺省的Activity,現(xiàn)在你應(yīng)該看到下面的畫面。
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上的是程序的標(biāo)題。Eclipse插件自動(dòng)創(chuàng)建它(這個(gè)字符串定義在res/values/strings.xml里,你的AndroidManifest.xml引用了它)。標(biāo)題下方的文字是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.
剛才這個(gè)例子使用的是一種被稱為可編程的UI布局。這意味著你直接在代碼里構(gòu)造程序的UI。如果你做了很多UI編程,你會(huì)對(duì)這種方式的繁瑣深有同感。
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構(gòu)建模型:基于XML的layout文件。下面這個(gè)例子的效果和剛才的一致:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello"/>
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文件的通常結(jié)構(gòu)非常簡單:它是一個(gè)XML元素的樹。每個(gè)node是一個(gè)View類的名稱。你可以在XML layout中使用任意從View派生的類的名字作為元素,包括自定義的。較之編程,這種方式易于快速構(gòu)建UI。這種模型受Web開發(fā)模型啟發(fā),這種方式下,程序的外觀和邏輯是分離的。
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例子里,只有一個(gè)View元素和五個(gè)XML屬性,下面是它們含義的總結(jié):
| 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 這個(gè)是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 這個(gè)屬性給TextView元素賦予了唯一的標(biāo)識(shí)符。你可以在代碼或者其他XML資源聲明中引用這個(gè)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 這個(gè)屬性定義了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 這個(gè)屬性設(shè)置了TextView要顯示的文本。在本例中,你使用字符串資源,而不是硬編碼。hello字符串定義在res/values/strings.xml里。這是在程序中添加字符串的推薦方法,因?yàn)檫@樣有助于多語言。. |
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/目錄。
Landscape layout
When you want a different design for landscape, put your layout XML file inside /res/layout-land. Android will automatically look here when the layout changes. Without this special landscape layout defined, Android will stretch the default 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插件自動(dòng)為你產(chǎn)生這些layout文件:main.xml。在剛才這個(gè)程序里,這個(gè)文件被忽略了,你用編程的方式創(chuàng)建了一個(gè)layout。這樣做主要是為了講解有關(guān)Android framwork,但是你應(yīng)該始終使用在XML里定義layout的方式而不是使用代碼。下面的過程將指導(dǎo)你把現(xiàn)在的代碼改變成使用XML layout。
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello"/>
Save the file.
轉(zhuǎn)載于:https://www.cnblogs.com/zz962/archive/2011/10/12/2209694.html
總結(jié)
以上是生活随笔為你收集整理的【译】Tutorials ---- Hello, World的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从三层架构到MVC
- 下一篇: Page 56 WPF布局原则