Android開發入門教程--Android應用程式結構分析

來源:互聯網
上載者:User

Android開發入門教程--Android應用程式結構分析

一、建立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     @Override10     protected void onCreate(Bundle savedInstanceState) {11         super.onCreate(savedInstanceState);12         setContentView(R.layout.activity_main);13     }14 15     @Override16     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 replaced33         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.34     35 36             Theme customizations available in newer API levels can go in37             res/values-vXX/styles.xml, while customizations related to38             backward-compatibility can go here.39         40 41         Base application theme for API 11+. This theme completely replaces42         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 replaces47         AppBaseTheme from BOTH res/values/styles.xml and48         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 – 布局檔案,雙擊activity_main.xml會進入可視化編輯介面,在這裡你可以根據需要選擇相應的控制項:

也可以點擊紅框部分進入文本編輯介面直接寫對應控制項的代碼(從代碼檔案可以看到該layout由一個相對布局和一個文字框組成):

AndroidManifest檔案:在應用程式目錄中倒數第四個可以看到一個AndroidManifest.xml檔案,它是應用程式的設定檔包含在每個安卓應用程式中,它向系統描述了本程式所包括的組件、所實現的功能、所能處理的資料、要請求的資源等,可以近似看做網站中的Web.conig檔案,同樣它也可以由可視化編輯器或文字編輯器編輯:

Android.jar檔案:Android.jar內部常用包作用概述,如所示:

瀏覽器操作介面
android.widget---------包含各種UI 元素(大部分是可見的)在應用程式的螢幕中使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.