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的声明周期

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

Android官方文档翻译 十五 3.3Supporting Different Platform Versions的更多相关文章

  1. Android官方文档翻译 十四 3.2Supporting Different Screens

    Supporting Different Screens 支持不同的屏幕 This lesson teaches you to 这节课教给你 Create Different Layouts 创建不同 ...

  2. Android官方文档翻译 十二 3.Supporting Different Devices

    Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...

  3. Android官方文档翻译 十六 4.Managing the Activity Lifecycle

    Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...

  4. Android官方文档翻译 十 2.3Styling the Action Bar

    Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...

  5. Android官方文档翻译 十八 4.2Pausing and Resuming an Activity

    Pausing and Resuming an Activity 暂停和恢复一个activity This lesson teaches you to 这节课教给你 Pause Your Activi ...

  6. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  7. Android官方文档翻译 四 1.2Running Your App

    Running Your App If you followed the previous lesson to create an Android project, it includes a def ...

  8. Android官方文档翻译 三 1.1Creating an Android Project

    Creating an Android Project 创建一个Android项目 An Android project contains all the files that comprise th ...

  9. Android官方文档翻译 五 1.3Building a Simple User Interface

    Building a Simple User Interface 创建一个简单的用户界面 This lesson teaches you to 这节课将教给你: Create a Linear Lay ...

随机推荐

  1. 选择…Select…(Power Query 之 M 语言)

    选择行: 筛选Table.SelectRows-文本与数值 筛选Table.SelectRows-日期与时间 保留错误行:= Table.SelectRowsWithErrors( 表, {" ...

  2. CF1481B New Colony 题解

    Content \(n\) 座山排成一行,其中第 \(i\) 座山的高度为 \(h_i\). 有 \(k\) 个巨石依次从第一座山开始滚落.当某个巨石在第 \(i\) 座山时: 如果 \(i=n\), ...

  3. java IO操作和计算操作:工作内存和主内存 volatile关键字作用;原子操作对象AtomicInteger ....

    应该停止但无法停止的计算线程 如下线程示例,线程实例中while循环中的条件,在主线程中通过调用实例方法更新后,while循环并没有更新判断变量是否还成立.而是陷入了while(true)死循环. i ...

  4. ORM-数据库命令操作包装实例对象学习

    http://www.cnblogs.com/alex3714/articles/5978329.html python 之路,Day11 - sqlalchemy ORM   本节内容 ORM介绍 ...

  5. Vue页面内公共的多类型附件图片上传区域并适用折叠面板

    在前端项目中,附件上传是很常用的功能,几乎所有的app相关项目中都会使用到,一般在选择使用某个前端UI框架时,可以查找其内封装好的图片上传组件,但某些情况下可能并不适用于自身的项目需求,本文中实现的附 ...

  6. jQuery Validate表单验证判断是否验证通过

    只判断某个字段是否验证通过,可以参考:https://www.cnblogs.com/pxblog/p/13801171.html <form action="" metho ...

  7. Caused by: redis.clients.jedis.exceptions.JedisDataException: READONLY You can't write against a read only slave.

    Caused by: redis.clients.jedis.exceptions.JedisDataException: READONLY You can't write against a rea ...

  8. 【九度OJ】题目1074:对称平方数 解题报告

    [九度OJ]题目1074:对称平方数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1074 题目描述: 打印所有不超过n( ...

  9. [数学]高数部分-Part I 极限与连续

    Part I 极限与连续 回到总目录 Part I 极限与连续 一.极限 泰勒公式 基本微分公式 常用等价无穷小 函数极限定义 数列极限数列极限 极限的性质 极限的唯一性 极限的局部有限性 极限的局部 ...

  10. [算法笔记-题解]问题 A: 例题4-1 一元二次方程求根

    问题 A: 例题4-1 一元二次方程求根 [命题人 : 外部导入] 时间限制 : 1.000 sec 内存限制 : 12 MB 题目描述 求一元二次方程ax2+bx+c=0的根,三个系数a, b, c ...