Android官方入门文档[11]支持不同平台版本
Supporting Different Platform Versions
支持不同平台版本
This lesson teaches you to
1.Specify Minimum and Target API Levels
2.Check System Version at Runtime
3.Use Platform Styles and Themes
You should also read
•Android API Levels
•Android Support Library
这节课教你
1.指定最小和目标API级别
2.检查系统版本在运行
3.使用平台的样式和主题
你也应该阅读
•Android的API级别
•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,你应该继续支持老版本的Android,直到更多的设备得到更新。本课向您展示如何利用最新的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 <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版本,它支持的细节。具体来说,minSdkVersion属性targetSdkVersion为<uses-sdk元素标识的最低API级别与您的应用程序是兼容的,最高的API水平,使你的设计和测试您的应用程序。
For example:
例如:
<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.
由于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提供了每个平台版本生成的常量类的唯一代码。使用你的应用程序中的这些代码来构建确保执行依赖于较高的API级别,只有当这些API可在系统上的代码的条件。
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”,你的应用程序包括在Android3.0及更高的动作条在默认情况下。为了再添加菜单项的动作吧,你需要设置的android:在你的菜单资源XML 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活动看起来像一个对话框:
<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 android:theme="@style/CustomTheme">
For more about creating and using themes, read the Styles and Themes guide.
欲了解更多有关创建和使用主题,阅读风格和主题指导。
Next class: Managing the Activity Lifecycle
下一们课程:管理活动的生命周期
本文翻译自:https://developer.android.com/training/basics/supporting-devices/platforms.html