App工程檔案分析
關於如何建立一個最簡單的Android App請參照連結:
《 Android學習筆記(一)環境安裝及第一個hello world 》 http://www.jb51.net/article/52593.htm
建立完的工程檔案如下圖所示,本文對一些主要的檔案進行分析。
src檔案分析
App源檔案如圖:
開啟源檔案 MainActivity.java 可看到如下代碼:
源碼主要功能如下:
App源檔案目錄
package com.example.firstapp;
匯入App所需的類
import android.os.Bundle; import android.app.Activity; import android.view.Menu;
MainActivity繼承自Activity
public class MainActivity extends Activity
重載onCreate方法,使用布局檔案初始化Activity
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);}
重載onCreateOptionsMenu方法,使用布局檔案初始化Menu
@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}
gen與res檔案
gen檔案夾下R.java檔案在建立工程時自動建立,為唯讀檔案,定義了項目所有資源的索引,裡面的每個靜態類都與一個資源對應。
例如:
1. 類drawable與res中包含drawable字樣的檔案夾關聯
2. 類layout與res中layout檔案夾關聯
3. 類menu與res中menu檔案夾關聯
res檔案夾下是App所使用的資源檔,其中:
1. drawable與icon相關
2. layout與布局相關
3. menu與menu布局相關
4. value字樣的定義了項目配置中使用的值
舉例: 介面中的文字
value的檔案夾下的strings.xml檔案中定義了名稱為hello_world的字串,其值為" hello world! "
layout檔案夾下的activity_main.xml中定義了Textveiw中的文字為hello_world字串。
Android Menifest.xml
App的主要設定檔,內容如下:
配置App資訊
package="com.example.firstapp"android:versionCode="1"android:versionName="1.0"
配置SDK等級
android:minSdkVersion="8"android:targetSdkVersion="19"
配置App資源
配置App的表徵圖、名稱及主題等,其資源與res檔案夾對應。
android:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"
配置App的Activity和App名稱
android:name="com.example.firstapp.MainActivity"android:label="@string/app_name"
配置App的intent-filter
action android:name="android.intent.action.MAIN"category android:name="android.intent.category.LAUNCHER"
最後
以上為App工程檔案分析,個人理解,僅供參考。