天天看點

Android官方文檔翻譯 十五 3.3Supporting Different Platform VersionsSupporting Different Platform Versions

Supporting Different Platform Versions

支援不同的平台版本

This lesson teaches you to

這節課教給你
  1. Specify Minimum and Target API Levels
    指定最小和目标API等級
  2. Check System Version at Runtime
    檢查運作時的系統版本
  3. Use Platform Styles and Themes
    使用平台樣式和主題

You should also read

你還應該閱讀
  • Android API Levels
    Android API等級
  • Android Support Library
    Android支援庫

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.

雖然最新版本的Android通常能為你的應用程式提供更好的API,但是在更多的裝置更新到最新版本之前你應該繼續讓你的應用程式支援舊版本。這節課講給你展現如何利用最新的API以及盡可能更好地支援舊版本。

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版本的機器的分布情況,它以通路谷歌Play商店的裝置數量以基礎作為統計。通常,支援90%以上的活躍機型就是一個很好的慣例,然而,還是應讓你的應用程式把最新版本作為目标。

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.

技巧:為了給衆多的Android版本提供最好的特性和功能,你應該在你的應用程式中使用Android支援類庫,它允許你在舊版本上使用一些最新開發的平台API。

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

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在建構常量類的時候為每個平台版本都提供了一份獨一無二的代碼。在你的應用程式中使用這些代碼來建構條件以確定僅支援在高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。為了往action bar上添加菜單資源,你需要在你的菜單資源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提供了一些使用者體驗主題,它是底層系統的應用程式的外觀和感受。這些主題可以通過manifest檔案應用在你的應用程式中。通過使用這些内置的樣式和主題,你的應用程式會随着每個Android新版本的發行,而自然地跟随這些最新的外觀和感受。

To make your activity look like a dialog box:

為了讓你的activity看起來想一個對話框:
<activity android:theme="@android:style/Theme.Dialog">
           

To make your activity have a transparent background:

為了讓你的activity有一個透明背景:
<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 element:

為了在你的應用程式(全部activity)中應用一個主題,在元素中添加android:theme屬性:
<application android:theme="@style/CustomTheme">
           

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

關于建立和使用主題的更多内容,請閱讀Styles and Themes 向導。

Next class: Managing the Activity Lifecycle

下一課:管理Activity的聲明周期

這是我自己翻譯的,如果您發現其中有重要錯誤,敬請指出,感謝!