Android开发入门教程--Android应用程序结构分析
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
一、新建HelloWorld項(xiàng)目:1、打開Eclipse,點(diǎn)擊“File”->"New"->"Project"-Android Application Project"":
在彈出的“New Android Application”窗體中輸入相應(yīng)的應(yīng)用名稱、項(xiàng)目名稱、包名稱,并選擇相應(yīng)的SDK版本和應(yīng)用主題:
選擇項(xiàng)目保存位置,一路“next”完成項(xiàng)目創(chuàng)建:
創(chuàng)建后的項(xiàng)目:
在創(chuàng)建后的項(xiàng)目名稱上右鍵單擊選擇“Run As”->“Android Application”運(yùn)行剛創(chuàng)建的項(xiàng)目:
 運(yùn)行結(jié)果:
二、應(yīng)用程序目錄結(jié)構(gòu)簡析:
1、應(yīng)用程序目錄結(jié)構(gòu):
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中的視圖部分,負(fù)責(zé)處理界面顯示。在MainActivity里面重寫了父類的onCreate方法和onCreateOptionsMenu方法,在重寫的onCreate方法里方法setContentView(R.layout.activity_main)給MainActivity設(shè)置了要顯示的視圖R.layout.activity_main,視圖由R類尋找并加載(感覺很像mvc,Activity相當(dāng)于Controller而要顯示的layout就相當(dāng)于具體的頁面)。
R文件:在MainActivity的setContentView(R.layout.activity_main)方法中我們用R.layout.activity_main指定了要顯示的視圖,在應(yīng)用程序目錄結(jié)構(gòu)的截圖中可以看到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文件里面有很多類,每個(gè)類里面又有很多變量,這些類和變量在我們添加、刪除控件或資源文件(圖片、聲音等)由開發(fā)工具自動(dòng)幫我們維護(hù)的,由它來調(diào)用應(yīng)用程序的各種資源,在代碼第一句的注釋中也說明了“AUTO-GENERATED FILE.? DO NOT MODIFY”。
layout文件:res/layout/activity_main.xml – 布局文件,雙擊activity_main.xml會(huì)進(jìn)入可視化編輯界面,在這里你可以根據(jù)需要選擇相應(yīng)的控件:
也可以點(diǎn)擊紅框部分進(jìn)入文本編輯界面直接寫對應(yīng)控件的代碼(從截圖代碼文件可以看到該layout由一個(gè)相對布局和一個(gè)文本框組成):
AndroidManifest文件:在應(yīng)用程序目錄截圖中倒數(shù)第四個(gè)可以看到一個(gè)AndroidManifest.xml文件,它是應(yīng)用程序的配置文件包含在每個(gè)安卓應(yīng)用程序中,它向系統(tǒng)描述了本程序所包括的組件、所實(shí)現(xiàn)的功能、所能處理的數(shù)據(jù)、要請求的資源等,可以近似看做網(wǎng)站中的Web.conig文件,同樣它也可以由可視化編輯器或文本編輯器編輯:
Android.jar文件:Android.jar內(nèi)部常用包作用概述,如下圖所示:
可以看到Android.jar里面包含了很多包,常見包的作用如下:
 android.app-----------提供高層的程序模型、提供基本的運(yùn)行環(huán)境
 android.content-------包含各種的對設(shè)備上的數(shù)據(jù)進(jìn)行訪問和發(fā)布的類
 android.database------通過內(nèi)容提供者瀏覽和操作數(shù)據(jù)庫
 android.graphics-------底層的圖形庫,包含畫布,顏色過濾,點(diǎn),矩形,可以將他們直接繪制到屏幕上.
 android.location-------定位和相關(guān)服務(wù)的類
 android.media---------提供一些類管理多種音頻、視頻的媒體接口
 android.net------------提供幫助網(wǎng)絡(luò)訪問的類,超過通常的java.net.* 接口
 android.os-------------提供了系統(tǒng)服務(wù)、消息傳輸、IPC 機(jī)制
 android.opengl--------提供OpenGL 的工具
 android.provider-------提供類訪問Android 的內(nèi)容提供者
 android.telephony-----提供與撥打電話相關(guān)的API 交互
 android.view-----------提供基礎(chǔ)的用戶界面接口框架
 android.util------------涉及工具性的方法,例如時(shí)間日期的操作
 android.webkit---------默認(rèn)瀏覽器操作接口
 android.widget---------包含各種UI 元素(大部分是可見的)在應(yīng)用程序的屏幕中使用
轉(zhuǎn)載于:https://my.oschina.net/pangzhuzhu/blog/318037
總結(jié)
以上是生活随笔為你收集整理的Android开发入门教程--Android应用程序结构分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: hadoop--集群时间同步(可不同步)
 - 下一篇: 表锁与行锁的区别以及适用情况