Android開發入門教程--Android應用程序結構分析
來源:程序員人生 發布時間:2014-09-08 12:13:10 閱讀次數:2670次
一、新建HelloWorld項目:
1、打開Eclipse,點擊“File”->"New"->"Project"-Android Application Project"":

在彈出的“New Android Application”窗體中輸入相應的應用名稱、項目名稱、包名稱,并選擇相應的SDK版本和應用主題:

選擇項目保存位置,一路“next”完成項目創建:

創建后的項目:

在創建后的項目名稱上右鍵單擊選擇“Run As”->“Android Application”運行剛創建的項目:

運行結果:

二、應用程序目錄結構簡析:
1、應用程序目錄結構:

2、各部分說明:
Activity文件:雙擊目錄中的“MainActivity.java”,可以看到MainActivity的代碼:
1 package android.basic.helloandroid;
2
3 import android.os.Bundle;
4 import android.app.Activity;
5 import android.view.Menu;
6
7 public class MainActivity extends Activity {
8
9 @Override
10 protected void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.activity_main);
13 }
14
15 @Override
16 public boolean onCreateOptionsMenu(Menu menu) {
17 // Inflate the menu; this adds items to the action bar if it is present.
18 getMenuInflater().inflate(R.menu.activity_main, menu);
19 return true;
20 }
21
22 }
從代碼中可以看到MainActivity繼承于Activity類,Activity是Android中的視圖部分,負責處理界面顯示。在MainActivity里面重寫了父類的onCreate方法和onCreateOptionsMenu方法,在重寫的onCreate方法里方法setContentView(R.layout.activity_main)給MainActivity設置了要顯示的視圖R.layout.activity_main,視圖由R類尋找并加載(感覺很像mvc,Activity相當于Controller而要顯示的layout就相當于具體的頁面)。
R文件:在MainActivity的setContentView(R.layout.activity_main)方法中我們用R.layout.activity_main指定了要顯示的視圖,在應用程序目錄結構的截圖中可以看到R文件位于gen目錄下面,雙擊顯示代碼:

1 /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 *
3 * This class was automatically generated by the
4 * aapt tool from the resource data it found. It
5 * should not be modified by hand.
6 */
7
8 package android.basic.helloandroid;
9
10 public final class R {
11 public static final class attr {
12 }
13 public static final class drawable {
14 public static final int ic_launcher=0x7f020000;
15 }
16 public static final class id {
17 public static final int menu_settings=0x7f070000;
18 }
19 public static final class layout {
20 public static final int activity_main=0x7f030000;
21 }
22 public static final class menu {
23 public static final int activity_main=0x7f060000;
24 }
25 public static final class string {
26 public static final int app_name=0x7f040000;
27 public static final int hello_world=0x7f040001;
28 public static final int menu_settings=0x7f040002;
29 }
30 public static final class style {
31 /**
32 Base application theme, dependent on API level. This theme is replaced
33 by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
34
35
36 Theme customizations available in newer API levels can go in
37 res/values-vXX/styles.xml, while customizations related to
38 backward-compatibility can go here.
39
40
41 Base application theme for API 11+. This theme completely replaces
42 AppBaseTheme from res/values/styles.xml on API 11+ devices.
43
44 API 11 theme customizations can go here.
45
46 Base application theme for API 14+. This theme completely replaces
47 AppBaseTheme from BOTH res/values/styles.xml and
48 res/values-v11/styles.xml on API 14+ devices.
49
50 API 14 theme customizations can go here.
51 */
52 public static final int AppBaseTheme=0x7f050000;
53 /** Application theme.
54 All customizations that are NOT specific to a particular API-level can go here.
55 */
56 public static final int AppTheme=0x7f050001;
57 }
58 }

從代碼中可以看到R文件里面有很多類,每個類里面又有很多變量,這些類和變量在我們添加、刪除控件或資源文件(圖片、聲音等)由開發工具自動幫我們維護的,由它來調用應用程序的各種資源,在代碼第一句的注釋中也說明了“AUTO-GENERATED FILE. DO NOT MODIFY”。
layout文件:res/layout/activity_main.xml
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈