點這裏下載:Android學習指南第二講源代碼html
Android系統從底向上一共分了4層,每一層都把底層實現封裝,並暴露調用接口給上一層。java
下面是簡單翻譯的版本:linux
接下來讓我帶領你們解析一個Android程序的各個組成部分,此次咱們拿一個Hello,World作例子,雖然只是一個Hello,World,但也是麻雀雖小五臟俱全,經過分析Hello,World的目錄結構,讓咱們對Android程序有一個總體全面的認識。android
(Lesson2_HelloWorld 顯示效果)程序員
(Lesson2_HelloWorld 目錄結構)web
Lesson2_HelloWorld程序的建立過程能夠參見教學視頻:第二講視頻:建立 Lesson2_HelloWorld數據庫
接下來咱們逐個部分加以講解:apache
一、Activity類 MainHelloWorld文件淺析瀏覽器
Activity是Android中的視圖部分,負責界面顯示。網絡
package android.basic.lesson2.helloworld;
import android.app.Activity;
import android.os.Bundle;public class MainHelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}能夠看到MainHelloWorld是Activity的子類,子類要重寫onCreate方法。setContentView(R.layout.main)方法是給Activity設置能夠顯示的視圖(View),視圖由R類負責尋找。
二、R文件淺析
咱們看到Gen目錄下有個R.Java文件,R文件由ADT自動生成,程序員不須要也不要去修改它,R文件負責調用應用程序中的非代碼資源。
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package android.basic.lesson2.helloworld;
public final class R {
public static final class attr {
}
public static final class color {
public static final int red=0x7f050000;
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int TextView01=0x7f060000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int tagline=0x7f040002;
}
}
從R文件中能夠看到每個資源都會有一個整數和它相對應。
三、res/layout/main.xml文件淺析 – 佈局layout
咱們看到有個res目錄,也就是resource目錄,這個目錄下存放資源文件,資源文件的統一管理,也是Android系統的一大特點。如今要注意看的是layout目錄下的main.xml。這個文件的內容是有關用戶界面佈局和設計的。在桌面程序設計領域採用XML也許比較新穎,可是在網頁設計領域,這個就很日常了。同窗們能夠用html來類比xml在佈局中的用途。
<?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」
android:gravity=」center」
>
<TextView
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:textColor=」#0f0″
android:textSize=」30px」
android:text=」@string/hello」
/>
<TextView
android:text=」@string/tagline」
android:id=」@+id/TextView01″
android:layout_width=」wrap_content」
android:textSize=」14px」
android:textColor=」@color/red」
android:layout_height=」wrap_content」>
</TextView>
</LinearLayout>
從以上代碼能夠看到整個程序界面由一個線性佈局控件(LinearLayout)和2個文本框控件(TextView)組成。res的其餘目錄裏的其餘文件也都是相關的資源描述。
四、AndroidManifest.xml文件淺析
在每一個應用程序的根目錄都會有一個AndroidManifest.xml文件,該文件向Android操做系統描述了,本程序所包括的組件,所實現的功能,能處理的數據,要請求的資源等等。學過Java Web開發的同窗能夠用Web應用程序裏的web.xml來類比這個AndroidManifest.xml文件。
<?xml version=」1.0″ encoding=」utf-8″?>
<manifest xmlns:android=」http://schemas.android.com/apk/res/android」
package=」android.basic.lesson2.helloworld」
android:versionCode=」1″
android:versionName=」1.0″>
<application android:icon=」@drawable/icon」 android:label=」@string/app_name」>
<activity android:name=」.MainHelloWorld」
android:label=」@string/app_name」>
<intent-filter>
<action android:name=」android.intent.action.MAIN」 />
<category android:name=」android.intent.category.LAUNCHER」 />
</intent-filter>
</activity></application>
<uses-sdk android:minSdkVersion=」8″ />
</manifest>咱們看到Manifest是根節點,節點屬性裏有versionCode和versionName來表示應用程序的版本;裏面能夠包含0個或1個application元素,application能夠包含多個activity組件等等,具體的內容咱們在接下來的課程裏詳細講解。
五、Android.jar文件淺析
做爲一個Java項目,一般狀況下都會引入要用到的工具類,也就是Jar包,在Android開發中,絕大部分開發用的工具包都被封裝到一個名叫Android.jar的文件裏了。若是咱們在Eclipse中展開來看,能夠看到j2se中的包,apache項目中的包,還有Android自身的包文件。在這裏咱們簡單瀏覽一下Android的包文件:
android.app :提供高層的程序模型、提供基本的運行環境
android.content :包含各類的對設備上的數據進行訪問和發佈的類
android.database :經過內容提供者瀏覽和操做數據庫
android.graphics :底層的圖形庫,包含畫布,顏色過濾,點,矩形,能夠將他們直接繪製到屏幕上.
android.location :定位和相關服務的類
android.media :提供一些類管理多種音頻、視頻的媒體接口
android.net :提供幫助網絡訪問的類,超過一般的java.net.* 接口
android.os :提供了系統服務、消息傳輸、IPC 機制
android.opengl :提供OpenGL 的工具
android.provider :提供類訪問Android 的內容提供者
android.telephony :提供與撥打電話相關的API 交互
android.view :提供基礎的用戶界面接口框架
android.util :涉及工具性的方法,例如時間日期的操做
android.webkit :默認瀏覽器操做接口
android.widget :包含各類UI 元素(大部分是可見的)在應用程序的屏幕中使用
本節課程就到這裏。