Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

AndroidManifest.xml 剖析(一):整體結構與 Activity 屬性

編輯:初階開發

對於許多 Android 開發者而言,全域設定檔 AndroidManifest.xml 是既熟悉又容易一知半解的部分。它幾乎貫穿每一個專案——無論你新增 Activity 還是 Service,都離不開這份設定檔。深入理解它的結構,不僅能簡化程式碼,也能顯著提升程式的可維護性。

整體結構

檔案最外層是 <manifest> 標籤,其中包含套件名稱(如 package="cn.android123.demo")、版本號 android:versionCode 與版本名稱 android:versionName。往內一層的 application 分支,則可能容納 Android 程式的四大元件:Activity、Service、Content Provider 與 Receiver。每當你新增上述任一元件,都必須在 AndroidManifest.xml 中對應加上一個節點。

Activity 的屬性

其中 Activity 最常用的屬性莫過於 android:name 與 android:label,但為了應付各種複雜場景,我們有必要了解它的完整屬性:

android:allowTaskReparenting=["true" | "false"]
android:alwaysRetainTaskState=["true" | "false"]
android:clearTaskOnLaunch=["true" | "false"]
android:configChanges=[one or more of: "mcc" "mnc" "locale"
                       "touchscreen" "keyboard" "keyboardHidden"
                       "navigation" "orientation" "fontScale"]
android:enabled=["true" | "false"]
android:excludeFromRecents=["true" | "false"]
android:exported=["true" | "false"]
android:finishOnTaskLaunch=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:launchMode=["multiple" | "singleTop" |
                    "singleTask" | "singleInstance"]
android:multiprocess=["true" | "false"]
android:name="string"
android:noHistory=["true" | "false"]
android:permission="string"
android:process="string"
android:screenOrientation=["unspecified" | "user" | "behind" |
                         "landscape" | "portrait" |
                         "sensor" | "nonsensor"]
android:stateNotNeeded=["true" | "false"]
android:taskAffinity="string"
android:theme="resource or theme"
android:windowSoftInputMode=[one or more of: "stateUnspecified"
                         "stateUnchanged" "stateHidden"
                         "stateAlwaysHidden" "stateVisible"
                         "stateAlwaysVisible" "adjustUnspecified"
                         "adjustResize" "adjustPan"] >

幾個特別值得留意的屬性:android:launchMode 決定了 Activity 的啟動模式,是理解任務與返回堆疊行為的關鍵;android:screenOrientation 用來鎖定螢幕方向;而 android:configChanges 則能讓你在旋轉螢幕等設定變動時自行接管處理,避免 Activity 被重建。掌握了這些屬性,日後遇到疑難雜症時往往能迎刃而解。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved