Android官方文档翻译 十五 3.3Supporting 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的声明周期

这是我自己翻译的,如果您发现其中有重要错误,敬请指出,感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值