2011年6月11日 星期六

Android 開發前須知 - 目錄結構


Android 預設的整體架構是以 MVC 的方式在做規劃,利用 MVC 的架構我們可以把運算邏輯、版面配置和控制元件獨立開來方便在開發過程中的修改更新,讓整個系統更有擴充的彈性,如果要利用到這些好處我們必須對其目錄結構有初步的認識。

在 Android 的開發目錄結構中有幾個較為重要分別是:
src, res, gen, R.java, AndroidManfest.xml

src目錄

專案的 package 和 source code 都會存放在此地


res -> values
專案的資源都可以在此定義,定義的格式為 xml 檔案,檔名可以利用資源類型做分類,像是利用 strings.xml 或是 colors.xml 我們可以很容易的判斷字串資源或是色彩資源,定義的方式為一組關於資源類型的標籤並且賦予其姓名,並且利用開始標籤與結束標籤將我們所需的值包覆。如果使用 Eclipse 作為開發環境,Google 提供了非常方便的編輯器,在編輯器可以直接利用 Add 或是 Remove 等按鍵產生需要的資源。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="white">#FFFFFF</drawable>
<drawable name="black">#000000</drawable>
</resources>



res -> layout -> main.xml
layout 裡面放置的是版面佈局的 xml 描述檔,當應用程式中所有需要用到的 UI 元件或是排版方式都必須在此檔案內做描述。如果使用 Eclipse 作為開發環境,Google 提供了非常方便的編輯器,可以利用拖拉以及點選參數填值的方式,輕鬆達到版面的佈置,而不需要進入xml檔案內做編寫。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/white" android:onClick="cvOnClick">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/KilogramsLabel" android:textColor="@drawable/black" android:text="@string/kgLabel"></TextView>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/kilograms" android:numeric="integer"></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ConverButton" android:text="@string/CvButton" android:onClick="cvOnClick"></Button>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/PondsLabel" android:text="@string/LbLabel" android:textColor="@drawable/black"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Ponds" android:textColor="@drawable/black"></TextView>
</TableRow>
</TableLayout>



gen -> R.java
此檔案為自動產生並且是唯讀檔,定義了所有資源的索引檔,假設當我們在 strings.xml 中加入了一個字串資源
<string name="hello">Hello World, HelloMyAndroid!</string>

當專案重整之後 R.java 將會自動的把放置 hello 這個名稱的資源位置給加入

/* 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 idv.tw.kudodir.hellomyandroid;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
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;
}
}


AndroidManfest.xml
當應用程式中有使用到任何的 Activity, Intent, Content Provider, Service 都必須在這個檔案內做描述。如果使用 Eclipse 作為開發環境,Google 提供了非常方便的編輯器,在編輯器內已經有許多預設好的欄位,只需要選擇欄位填入適當的值,就可以產生出。

0 意見:

張貼留言

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger