2.3 Supporting Different Platform Versions 支持不一樣的平臺版本

While the latest versions of Android often provide great APIs for your app, you should continue to support older versions of Android until more devices get updated. This lesson shows you how to take advantage of the latest APIs while continuing to support older versions as well.html

雖然最新版本的Android一般爲您的應用程序提供了很棒的API,但應該繼續支持較舊版本的Android,直到更新設備更新。 本課程向您展現如何利用最新的API,同時繼續支持舊版本。android

The dashboard for Platform Versions is updated regularly to show the distribution of active devices running each version of Android, based on the number of devices that visit the Google Play Store. Generally, it’s a good practice to support about 90% of the active devices, while targeting your app to the latest version.安全

平臺版本的儀表板會按期更新,以顯示運行每一個版本的Android的活動設備的分佈狀況,具體取決於訪問Google Play商店的設備數量。 通常來講,在將應用程序定位到最新版本的同時,支持大約90%的活動設備是一個很好的作法。app

Tip: In order to provide the best features and functionality across several Android versions, you should use the Android Support Library in your app, which allows you to use several recent platform APIs on older versions.less

提示:爲了在多個Android版本中提供最佳功能和功能,您應該在應用程序中使用Android支持庫,從而容許您在舊版本上使用幾種最新的平臺API。ide

Specify Minimum and Target API Levels

指定最低和目標API級別

The AndroidManifest.xml file describes details about your app and identifies which versions of Android it supports. Specifically, the minSdkVersion and targetSdkVersion attributes for the <uses-sdk> element identify the lowest API level with which your app is compatible and the highest API level against which you’ve designed and tested your app.測試

AndroidManifest.xml文件描述了您的應用程序的詳細信息,並標識了其支持的Android版本。 具體來講,<uses-sdk>元素的minSdkVersiontargetSdkVersion屬性標識了您的應用程序兼容的最低API級別,以及您設計和測試應用程序的最高API級別。ui

For example:this

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
    ...
</manifest>

As new versions of Android are released, some style and behaviors may change. To allow your app to take advantage of these changes and ensure that your app fits the style of each user's device, you should set the targetSdkVersion value to match the latest Android version available.google

隨着新版Android的發佈,一些風格和行爲可能會改變。 爲了讓您的應用程序可以利用這些更改,並確保您的應用程序適合每一個用戶設備的風格,您應該將targetSdkVersion值設置爲與最新版本的Android版本相匹配。

Check System Version at Runtime

在運行時檢查系統版本

Android provides a unique code for each platform version in the Build constants class. Use these codes within your app to build conditions that ensure the code that depends on higher API levels is executed only when those APIs are available on the system.

Android在Build常量類中爲每一個平臺版本提供了一個惟一的代碼。 在您的應用程序中使用這些代碼來構建條件,以確保僅在系統上提供這些API時才執行依賴於更高API級別的代碼。

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);
    }
}

Note: When parsing XML resources, Android ignores XML attributes that aren’t supported by the current device. So you can safely use XML attributes that are only supported by newer versions without worrying about older versions breaking when they encounter that code. For example, if you set the targetSdkVersion="11", your app includes the ActionBar by default on Android 3.0 and higher. To then add menu items to the action bar, you need to set android:showAsAction="ifRoom" in your menu resource XML. It's safe to do this in a cross-version XML file, because the older versions of Android simply ignore the showAsAction attribute (that is, you do not need a separate version in res/menu-v11/).

注意:解析XML資源時,Android會忽略當前設備不支持的XML屬性。 所以,您能夠安全地使用僅由較新版本支持的XML屬性,而沒必要擔憂在遇到該代碼時舊版本中斷。 例如,若是您設置targetSdkVersion="11",則應用程序在Android 3.0及更高版本上默認包含ActionBar。 而後將菜單項添加到操做欄,您須要在菜單資源XML中設置android:showAsAction="ifRoom"。 能夠在跨版本的XML文件中作到這一點,由於舊版本的Android只是忽略showAsAction屬性(也就是說,在res/menu-v11/中不須要單獨的版本)。

Use Platform Styles and Themes

使用平臺樣式和主題

Android provides user experience themes that give apps the look and feel of the underlying operating system. These themes can be applied to your app within the manifest file. By using these built in styles and themes, your app will naturally follow the latest look and feel of Android with each new release.

Android提供用戶體驗主題,爲應用程序提供底層操做系統的外觀和感受。 這些主題能夠應用到清單文件中的應用程序。 經過使用這些內置的樣式和主題,您的應用程序將天然地遵循Android的最新外觀和每一個新版本的感受。

To make your activity look like a dialog box:

要使您的活動看起來像一個對話框:

<activity android:theme="@android:style/Theme.Dialog">

To make your activity have a transparent background:

要使您的活動具備透明背景:

<activity android:theme="@android:style/Theme.Translucent">

To apply your own custom theme defined in /res/values/styles.xml:

要應用您定製在/res/values/styles.xml中的自定義主題:

<activity android:theme="@style/CustomTheme">

To apply a theme to your entire app (all activities), add the android:theme attribute to the <application> element:

要將主題應用於整個應用程序(全部活動),請將android:theme屬性添加到<application>元素中:

<application android:theme="@style/CustomTheme">

For more about creating and using themes, read the Styles and Themes guide.

有關建立和使用主題的更多信息,請閱讀Styles and Themes指南。

相關文章
相關標籤/搜索