多设备官方教程(6)控制多版本API
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.
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.
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.
Specify Minimum and Target API Levels
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.
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.
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.
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/
).
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.
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
:
<activity android:theme="@style/CustomTheme">
To apply a theme to your entire app (all activities), add the android:theme
attribute to the <application>
element:
<application android:theme="@style/CustomTheme">
For more about creating and using themes, read the Styles and Themes guide.
多设备官方教程(6)控制多版本API的更多相关文章
- Spring 官方教程:使用 Restdocs 创建 API 文档
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247483998&idx=1&sn=6ae5fa795d36b1 ...
- MVC4学习之官方教程中迁移版本库报错
因工作需要,学习MVC4,但是微软官方教程中迁移版本库步骤在本地测试报错 官方教程地址:http://www.asp.net/mvc/overview/older-versions/getting-s ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...
- 详解 “Android UI”设计官方教程
我们曾经给大家一个<MeeGo移动终端设备开发UI设计基础教程>,同时很多朋友都在寻找Android UI开发的教程,我们从Android的官方开发者博客找了一份幻灯片,介绍了一些Andr ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(1)-官方教程The Profiler window翻译
本文是Unity官方教程,性能优化系列的第一篇<The Profiler window>的简单翻译. 相关文章: Unity性能优化(1)-官方教程The Profiler window翻 ...
- 微信公开课发布微信官方教程:教你用好微信JS-SDK接口
微信公众平台开放JS-SDK(微信内网页开发工具包),说明文档已经有相关使用方法和示例了,很多同学觉得不是很直观,为此微信公开课发布微信官方教程:教你用好微信JS-SDK接口. 1.分享类接口:支持获 ...
- 微软RPC官方教程
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379010(v=vs.85).aspx 注意:原文版本较老,我更新和改变了部分内容 ...
- Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译
本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
随机推荐
- .net远程连接oracle数据库不用安装oracle客户端
asp.net远程连接oracle数据库不用安装oracle客户端的方法下面是asp.net连接远程Oracle数据库服务器步骤: 1.asp.net连接oracle服务器需要添加Sytem.Data ...
- SDL_Test库(1)——SDL不用TTF库绘制文字
SDL库有很多的扩展,这很方便.但是每个扩展库都很臃肿,一般都会拖上额外的两三个开源库,更有甚者,扩展库的大小比SDL库本身还大得多.但有一个自带的.很有用的库很容易被大家忽视.它就是本文要讲的SDL ...
- 问题解决:form表单的button按钮问题
最近写了一个KendoUI 的kendowindow组件,组件里包含了一个form表单,表单里有一个button,不是提交保存的按钮,而是链接到另一个kendowindow的按钮,跟请求提交毫无关系, ...
- NOIP200905
暴力,简单,,, 题目好长,差点没读完 试题描述 R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历经艰险后,潜伏于S国的R国间谍小C终于摸清了S国军用密码的编码规则:1)S国军方内 ...
- OpenJudge 2754 八皇后
1.链接地址: http://bailian.openjudge.cn/practice/2754 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 会下国际象棋的人都很清楚: ...
- 改善EF代码的方法(下)
本节,我们将介绍一些改善EF代码的方法,包括编译查询.存储模型视图以及冲突处理等内容. > CompiledQuery 提供对查询的编译和缓存以供重新使用.当相同的查询需要执行很多遍的时候,那么 ...
- the evaluation period for visual studio trial edition has ended的解决方法-转发
首先献上自己收集的Visual studio 2008序列号: Visual Studio 2008 Professional Edition: XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4 ...
- Web前端新人笔记之CSS值和单位
数字 颜色——命名颜色 在Css2.1中规范定义了17个颜色名.包括html4.0中定义的16个颜色及外加一个橙色: <h1 style="color=aqua">aq ...
- LPC17XX 数据手册摘要之系统时钟与功率控制
系统时钟与功率控制 一.系统时钟 LPC17XX有三个独立的时钟振荡器,分别是主振荡器(MIAN_OSC).内部RC振荡器(IRC_OSC).实时时钟振荡器(RTC_OSC).LPC17XX时钟框图如 ...
- 【原】.Net之美学习笔记-第1章-1.1.1值类型
结构还有一个特性:调用结构上的方法前,需要对其所有的字段进行赋值.