Android開發之道(4)程式框架基礎

鍾超發表於2012-02-08

轉載請註明來自“柳大的CSDN部落格”:http://blog.csdn.net/poechant



1、程式框架


對於一個最簡單的HelloWorld應用,程式的檔案結構如下:




可以看到有5個目錄:src原始檔目錄、gen目錄、assets目錄、bin目錄、res目錄。另外還在根目錄下有三個檔案,分別是AndroidManifest.xmlproguard.cfgproject.properties。下面分別介紹它們的作用。


1)原始檔目錄src:這個不需要多做解釋,原始檔都包含在這個目錄下。

2)索引檔案目錄gengen就是generated的縮寫,其中的檔案都是在建立專案的時候自動生成的,其中有R.java檔案。它是一個只讀檔案,其中定義了專案中所有資源的索引。

3)資原始檔目錄res:該目錄包含程式圖示drawable(如drawable-hdpidrawable-ldpidrawable-mdpi中的ic_lancher.png),佈局檔案layout(如main.xml),常量values(如strings.xml)。

4)可執行檔案目錄bin:包括apk檔案、dex檔案和ap_檔案,還包括不被封裝到可執行檔案中的一些資源(比如圖片等)。

5assets目錄。


2、具有資源索引功能的R.java檔案(R即Resource)


package com.sinosuperman.android;

public final class R {

    // indices for attributes
        public static final class attar {
    }

    // indices for drawable files
    public static final class drawable {
        public static final int ic_launcher = 0x7f020000;
    }

    // indices for layout files
    public static final class layout {
        public static final int main = 0x7f030000;
    }

    // indices for strings
    public static final class string {
    public static final int app_name = 0x7f040001;
    public static final int hello = 0x7f040000;
    }
}


3、程式框架


Android應用程式中的AndroidManifest.xml檔案定義了整個程式的框架,最簡單的HelloWorld的框架如下:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.sinosuperman.android"
	android:versionCode="1"
	android:versionName="1.0"

	<uses-sdk android:minSdkVersion="8" />

	<application
		android:icon="@drawable/ic_launcher"
		android:label="@string/app_name"
		<activity
			android:name=".HelloWorldActivity"
			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>

</manifest>


根節點manifest包含package中所有內容,package屬性的值是com.sinosuperman.android。根節點manifest包含兩個子節點,分別是uses-sdkapplicationuses-sdkandroid:minSdkVersion屬性表明執行該程式所需的最低版本的Android SDK8application節點有兩個屬性,分別是android:iconandroid:label,表示應用圖示和應用名稱,它還包含一個子節點,是activity,它是應用程式預設啟動的activityactivity節點有兩個屬性,android:nameactivity的名稱,android:label是應用名稱。application的子節點是intent-filter,它又包含兩個子節點,分別是actioncategoryactionandroid:name屬性是android.intent.action.MAINcategoryandroid:name屬性是android.intent.category.LAUNCHER


可見一個最基本的AndroidManifest.xml檔案有5層節點,第一層是manifest,第二層是uses-sdkapplicationappcalition下面有第三層的activityactivity下面有第四層的intent-filterintent-filter下面有第五層的actioncategory。可以發現除了manifestxmlns:androidpackage屬性外,其他各屬性均是在android名字空間(namespace)下的。


4、常量


Android應用中,所有的常量都是通過res/values/strings.xml來定義的,該檔案如下:


<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="hello">Hello World, HelloWorldActivity!</string>
	<string name="app_name">HelloWorld</string>
</resources>


這個檔案中定義了兩個資源(resources),分別是兩個名為helloapp_namestringsR.java中的String內部類裡面有兩個int成員,它們以索引的形式,分別指向res/values/strings.xml中的這兩個string節點。如何使用這些常量呢?


Resources res = this.getContext().getResources();
String appname = ((String) res.getString(R.string.app_name));
String hello = ((String) res.getString(R.string.hello));


5、佈局檔案


Android應用程式中的佈局由res/layout/main.xml檔案指定,如下是HelloWorld應用的佈局檔案:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="file_parent"
	android:orientation="vertical" >

	<TextView
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="@string/hello" />

</LinearLayout>


從名字我們就可以看出來,這是一個線性排版的佈局。其android:layout_widthandroid:layout_height屬性指定了該佈局所使用的長和寬,file_parent表示在與上層所在容器的內層尺寸相同。android:orientation屬性指定元件的排列方式,vertical表示垂直排列,horizontal表示水平排列。

LinearLayout有一個TextView子節點,它用來配置文字標籤元件。它的寬屬性設定的是fill_parent,高屬性是wrap_content,表示隨著文字欄位的不同而改變這個檢視的高度。android:text指定了顯示的內容,這裡設定的是常量資源中的hello,即“Hello World, HelloWorldActivity!”


6、程式原始檔


接好程式框架、資原始檔和佈局檔案後,我們就可以開始著手瞭解原始檔了。如下是AndroidHello World應用的原始檔:


package com.sinosuperman.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends Acitity {
	/* Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		tv.setText("Hello World!!!");
		setContentView(tv);
	}
}


我們來簡單解釋一下這個程式。我們所建立的HelloWorldActivity類,繼承了Activity類,Override了它的onCreate方法。該方法有一個Bundle型別的引數savedInstanceState。該方法中,先呼叫ActivityonCreate,然後建立一個TextView元件,再用setContentView來設定它為佈局中要顯示的元件。


7proguard.cfg


簡單說,proguard.cfgProGuard來提供防止你的應用被反編譯的機制。ProGuard是一個開源專案,其主頁是:http://proguard.sourceforge.net/通過一些編碼混淆、重新命名等方式。另外ProGuard還可以縮小你的程式碼量、移除無用程式碼。一方面為了給予你的應用一個合理的身材,又防止反編譯你的程式碼,使用ProGuard提供的保護機制是非常有必要的。

應用在建立之初,會自動建立proguard.cfg檔案。但是它只考慮了最普遍的情況,你要根據你自己的應用來寫proguard.cfg配置。由於HelloWorld應用的簡單,這裡不詳述。後續會介紹proguard.cfg的寫法。


8project.properties


簡單的HelloWorld應用中,該檔案如下:


# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "ant.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-8

這可以看到它標示了應用開基於的Android版本。注視中提示對這個檔案的修改不會其作用,該檔案由Android開發工具自動產生,可以建立Java開發者熟悉的ant.properties來管理你的開發專案。通過target變數,來對應用進行Android開發版本控制。


9assets目錄


Android專案中的assets目錄與res類似,最大的區別是res目錄會為資源生成索引使用的ID,assets不會。



到這裡,就簡要分析完了HelloWorld程式 : )


轉載請註明來自“柳大的CSDN部落格”:http://blog.csdn.net/poechant

-



相關文章