標籤:
一:建立項目
File-New-Android Application Project
圖:HelloAndroid的項目
src檔案夾
src:存放項目的源碼,在src檔案夾中,系統為我們自動建立了MainActivity.java
package com.example.helloandroid;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView t=new TextView(this); t.setText("hello world!"); setContentView(t); } @Override public 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; } }
其中匯入了兩個類android.app.Activity和android.os.Bundle,import android.view.Menu; import android.widget.TextView; 是在TextView下添加的,在其中重寫了父類的兩個方法
@Override
在重寫父類的onCreate時,在方法前面加上@Override 系統可以幫你檢查方法的正確性。例如,public void onCreate(Bundle savedInstanceState){…….}這種寫法是正確的,如果你寫成public void oncreate(Bundle savedInstanceState){…….}這樣編譯器回報如下錯誤——The method oncreate(Bundle) of type HelloWorld must override or implement a supertype method,以確保你正確重寫onCreate方法。(因為oncreate應該為onCreate)
而如果你不加@Override,則編譯器將不會檢測出錯誤,而是會認為你新定義了一個方法oncreate。
android.app.Activity類:為幾乎所有的活動(activities)都是與使用者互動的,你可以用方法setContentView(View)將自己的UI放到裡面,有兩個方法是幾乎所有的Activity子類都實現的:
onCreate(Bundle):初始化你的活動(Activity),比如完成一些圖形的繪製。最重要的是,在這個方法裡你通常將用布局資源(layout resource)調用setContentView(int)方法定義你的UI,和用findViewById(int)在你的UI中檢索你需要編程地互動的小組件(widgets)。setContentView指定由哪個檔案指定布局(main.xml),可以將這個介面顯示出來,然後我們進行相關操作,我們的操作會被封裝成為一個意圖,然後這個意圖對應有相關的activity進行處理。
onPause():處理當離開你的活動時要做的事情。最重要的是,使用者做的所有改變應該在這裡提交(通常ContentProvider儲存資料)。
gen檔案夾
其下有兩個java檔案。R.java檔案中定義了一個類——R,R類中包含很多靜態類,且靜態類的名字都與res中的一個名字對應,即R類定義該項目所有資源的索引
public final class R { public static final class attr { } public static final class dimen { /** Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.xml (such as screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. */ public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080000; } public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int app_name=0x7f050000; public static final int hello_world=0x7f050002; } public static final class style { /** Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. Base application theme for API 11+. This theme completely replaces AppBaseTheme from res/values/styles.xml on API 11+ devices. API 11 theme customizations can go here. Base application theme for API 14+. This theme completely replaces AppBaseTheme from BOTH res/values/styles.xml and res/values-v11/styles.xml on API 14+ devices. API 14 theme customizations can go here. */ public static final int AppBaseTheme=0x7f060000; /** Application theme. All customizations that are NOT specific to a particular API-level can go here. */ public static final int AppTheme=0x7f060001; }}
通過R.java我們可以很快地尋找我們需要的資源,另外編繹器也會檢查R.java列表中的資源是否被使用到,沒有被使用到的資源不會編繹進軟體中,這樣可以減少應用在手機佔用的空間。
Android Private Libraries檔案夾
該檔案夾下包括android-support-v4.jar,一個Java 歸檔檔案,其中包含構建應用程式所需的所有的Android SDK 庫(如Views、Controls)和APIs。通過android-support-v4.jar,將自己的應用程式綁定到Android SDK和Android Emulator,這允許你使用所有Android的庫和包,且使你的應用程式在適當的環境中調試,例如上文中使用的MainActivity.java使用的類庫
import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;
都來自上面的android-support-v4.jar
assets檔案夾
包含應用系統需要使用到的諸如mp3、視頻類的檔案。
res檔案夾
資來源目錄,包含你項目中的資源檔並將編譯進應用程式。向此目錄添加資源時,會被R.java自動記錄。建立一個項目,res目錄下會有三個子目錄:drawabel、layout、values。
AndroidManifest.xml
項目的總設定檔,記錄應用中所使用的各種組件。個檔案列出了應用程式所提供的功能,在這個檔案中,你可以指定應用程式使用到的服務(如電話語音、互連網服務、簡訊服務、GPS服務等等)。另外當你新添加一個Activity的時候,也需要在這個檔案中進行相應配置,只有配置好後,才能調用此Activity。AndroidManifest.xml將包含如下設定:application permissions、Activities、intent filters等。
如果你跟我一樣是ASP.NET出生或者學過,你會發現AndroidManifest.xml跟web.config檔案很像,可以把它類同於web.config檔案理解。
如果你不是,你可以這樣理解——眾所周知xml是一種資料交換格式,AndroidManifest.xml就是用來儲存一些資料的,只不過這些資料時關於android項目的配置資料。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloandroid" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.helloandroid.MainActivity" 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>
default.properties
記錄項目中所需要的環境資訊,比如Android的版本等
二:配置模擬器windows→Android Virtual Device Manager
配置好虛擬器後就可以編譯運行我們的的第一個HelloWorld程式了
run as →Android Appilcation
參考文獻
- http://www.cnblogs.com/skynet/archive/2010/04/13/1711479.html
Android開發筆記1之HelloWorld