Android學習筆記(二)App工程檔案分析_Android

來源:互聯網
上載者:User

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工程檔案分析,個人理解,僅供參考。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.