支援不同的平台版本
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
雖然最新版的 Android 常能為應用程式提供卓越的 API,但仍應繼續支援舊版 Android,直到更多裝置完成更新為止。本課程將說明如何使用最新的 API,同時繼續支援舊版。
使用 Android Studio「New Project」精靈找出搭載各 Android 版本的有效裝置分布情形。這個分布資料是根據造訪 Google Play 商店的裝置數量統計而來。一般來說,我們建議支援約 90% 的有效裝置,同時指定應用程式為最新版本。
提示:如要在多個 Android 版本之間提供最佳功能,請使用應用程式的「Android 支援資料庫」,以便在較舊版本中使用數個最新平台的 API。
指定最小 API 級別和目標 API 級別
AndroidManifest.xml 檔案會描述應用程式的詳細資料,並指出應用程式支援的 Android 版本。具體來說,<uses-sdk>
元素的 minSdkVersion
和 targetSdkVersion
屬性可識別應用程式相容的最低 API 級別,以及應用程式相容性和您設計且已測試過的最高 API 等級。
例如:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
...
</manifest>
隨著新版本的 Android 推出,某些樣式和行為可能會有所變動。
為了讓應用程式能夠充分運用這些異動,並確保應用程式符合每位使用者的裝置樣式,請配合當下可用的最新 Android 版本設定 targetSdkVersion
值。
由於 SDK 子版本與行為變更無關,因此無法設定 targetSdkVersion
來反映 SDK 子版本。如要呼叫比 minSdkVersion
更新的 SDK 子版本中的 API,請在執行階段檢查系統版本。
在執行階段檢查系統版本
Android 會為 Build
常數類別中的每個平台版本提供專屬代碼。請在應用程式中使用這些程式碼來建構條件,確保只有在系統可使用這些 API 時,才會執行需要較高 API 級別的程式碼。
Kotlin
private fun setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
actionBar.setDisplayHomeAsUpEnabled(true)
}
}
Java
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
您可以使用 Build.VERSION.SDK_INT_FULL 檢查主要或次要 SDK 版本是否存在。
Kotlin
if (SDK_INT_FULL >= VERSION_CODES_FULL.[MAJOR or MINOR RELEASE]) {
// Use APIs introduced in a major or minor SDK release
}
Java
if (SDK_INT_FULL >= VERSION_CODES_FULL.[MAJOR or MINOR RELEASE]) {
// Use APIs introduced in a major or minor SDK release
}
注意:剖析 XML 資源時,Android 會忽略目前裝置不支援的 XML 屬性。因此,您可以放心地使用新版支援的 XML 屬性,不必擔心這些舊版本會中斷程式。舉例來說,如果您設定了 targetSdkVersion="11"
,您的應用程式就會在 Android 3.0 以上版本中預設納入 ActionBar
。如要將選單項目新增至動作列,您必須在選單資源 XML 中設定 android:showAsAction="ifRoom"
。在跨版本 XML 檔案中執行這項操作可避免發生問題,原因是舊版 Android 會忽略 showAsAction
屬性 (也就是說,您「不需要」在 res/menu-v11/
有獨立版本)。
使用平台樣式和主題
Android 提供使用者體驗主題,能讓應用程式提供作業系統的外觀和風格。這些主題可套用至資訊清單檔案中的應用程式。只要使用這些內建樣式和主題,您的應用程式在發布新版本時自然就會獲得 Android 的最新外觀和風格。
如要讓活動看起來像對話方塊:
<activity android:theme="@android:style/Theme.Dialog">
為了讓活動呈現透明背景:
<activity android:theme="@android:style/Theme.Translucent">
如何套用在 /res/values/styles.xml
中定義的自訂主題:
<activity android:theme="@style/CustomTheme">
如要為整個應用程式 (所有活動) 套用主題,請在 <application>
元素中加入 android:theme
屬性:
<application android:theme="@style/CustomTheme">
如要進一步瞭解如何建立及使用主題,請參閱「樣式和主題」指南。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-20 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-20 (世界標準時間)。"],[],[],null,["# Support different platform versions\n\nWhile the latest versions of Android often provide great APIs for your app, you should continue\nto support older versions of Android until more devices get updated. This\nlesson shows you how to take advantage of the latest APIs while continuing to support older\nversions as well.\n\nUse the Android Studio **New Project** wizard to find the distribution of active\ndevices running each version of Android. This distribution is based on the number of devices that\nvisit the Google Play Store. Generally, we recommend supporting about 90% of active devices, while\ntargeting your app to the latest version.\n\n**Tip:** In order to provide the best features and\nfunctionality across several Android versions, you should use the [Android Support Library](/tools/support-library) in your app,\nwhich allows you to use several recent platform APIs on older versions.\n\nSpecify minimum and target API levels\n-------------------------------------\n\nThe [AndroidManifest.xml](/guide/topics/manifest/manifest-intro) file\ndescribes details about your app and\nidentifies which versions of Android it supports. Specifically, the `minSdkVersion`\nand `targetSdkVersion` attributes for the [`\u003cuses-sdk\u003e`](/guide/topics/manifest/uses-sdk-element) element\nidentify the lowest API level with which your app is compatible and the highest API level against\nwhich you've designed and tested your app.\n\nFor example: \n\n```xml\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\" ... \u003e\n \u003cuses-sdk android:minSdkVersion=\"4\" android:targetSdkVersion=\"15\" /\u003e\n ...\n\u003c/manifest\u003e\n```\n\nAs new versions of Android are released, some style and behaviors may change.\nTo allow your app to take advantage of these changes and ensure that your app fits the style of\neach user's device, you should set the\n[`targetSdkVersion`](/guide/topics/manifest/uses-sdk-element#target)\nvalue to match the latest Android version\navailable.\n\nSince minor SDK versions are not tied to behavior changes, it's not possible to set\n`targetSdkVersion` to reflect a minor SDK version. If you want to call an API in a minor\nSDK version that is more recent than your `minSdkVersion`, check the\n[system version at runtime](/training/basics/supporting-devices/$version-codes).\n\nCheck system version at runtime\n-------------------------------\n\nAndroid provides a unique code for each platform version in the [Build](/reference/android/os/Build)\nconstants class. Use these codes within your app to build conditions that ensure the code that\ndepends on higher API levels is executed only when those APIs are available on the system. \n\n### Kotlin\n\n```kotlin\nprivate fun setUpActionBar() {\n // Make sure we're running on Honeycomb or higher to use ActionBar APIs\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.HONEYCOMB) {\n actionBar.setDisplayHomeAsUpEnabled(true)\n }\n}\n```\n\n### Java\n\n```java\nprivate void setUpActionBar() {\n // Make sure we're running on Honeycomb or higher to use ActionBar APIs\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.HONEYCOMB) {\n ActionBar actionBar = getActionBar();\n actionBar.setDisplayHomeAsUpEnabled(true);\n }\n}\n```\n\nYou can use [Build.VERSION.SDK_INT_FULL](/reference/android/os/Build.VERSION#SDK_INT_FULL)\nto check for the presence of either a major or minor SDK version. \n\n### Kotlin\n\n```kotlin\nif (SDK_INT_FULL \u003e= VERSION_CODES_FULL.[MAJOR or MINOR RELEASE]) {\n // Use APIs introduced in a major or minor SDK release\n}\n```\n\n### Java\n\n```java\nif (SDK_INT_FULL \u003e= VERSION_CODES_FULL.[MAJOR or MINOR RELEASE]) {\n // Use APIs introduced in a major or minor SDK release\n}\n```\n\n**Note:** When parsing XML resources, Android ignores XML\nattributes that aren't supported by the current device. So you can safely use XML attributes that\nare only supported by newer versions without worrying about older versions breaking when they\nencounter that code. For example, if you set the\n`targetSdkVersion=\"11\"`, your app includes the [ActionBar](/reference/android/app/ActionBar) by default\non Android 3.0 and higher. To then add menu items to the action bar, you need to set\n`android:showAsAction=\"ifRoom\"` in your menu resource XML. It's safe to do this\nin a cross-version XML file, because the older versions of Android simply ignore the\n`showAsAction` attribute (that is, you *do not* need a separate\nversion in `res/menu-v11/`).\n\nUse platform styles and themes\n------------------------------\n\nAndroid provides user experience themes that give apps the look and feel of the\nunderlying operating system. These themes can be applied to your app within the\nmanifest file. By using these built in styles and themes, your app will\nnaturally follow the latest look and feel of Android with each new release.\n\nTo make your activity look like a dialog box: \n\n```xml\n\u003cactivity android:theme=\"@android:style/Theme.Dialog\"\u003e\n```\n\nTo make your activity have a transparent background: \n\n```xml\n\u003cactivity android:theme=\"@android:style/Theme.Translucent\"\u003e\n```\n\nTo apply your own custom theme defined in `/res/values/styles.xml`: \n\n```xml\n\u003cactivity android:theme=\"@style/CustomTheme\"\u003e\n```\n\nTo apply a theme to your entire app (all activities), add the `android:theme`\nattribute\nto the [`\u003capplication\u003e`](/guide/topics/manifest/application-element) element: \n\n```xml\n\u003capplication android:theme=\"@style/CustomTheme\"\u003e\n```\n\nFor more about creating and using themes, read the [Styles and Themes](/guide/topics/ui/themes) guide."]]