多版本支持 API版本 运行环境检查 风格和主题
2016-01-20 00:21
288 查看
Supporting Different Platform Versions
上一课下一课This lesson teaches you to
Specify Minimum and Target API LevelsCheck System Version at Runtime
Use Platform Styles and Themes
You should also read
Android API LevelsAndroid Support Library
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, theminSdkVersionand
targetSdkVersionattributes for the
<uses-sdkelement 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
targetSdkVersionvalue to match the latest Android version available.
Check System Version at Runtime
Android provides a unique code for each platform version in theBuildconstants 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
ActionBarby 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
showAsActionattribute (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:themeattribute to the
<application>element:
<application android:theme="@style/CustomTheme">
For more about creating and using themes, read the Styles and Themes guide.
相关文章推荐
- shell判断字符串为空
- Android的WebView中js与java代码相互调用
- 如何优化cocos2d/x程序的内存使用和程序大小
- Android中的Handler机制
- 计算数组的逆序对个数
- DeprecationWarning: the sets module is deprecated
- 锁帧同步算法
- epoll为什么这么快,epoll的实现原理
- Swift语法基础入门四(构造函数, 懒加载)
- htons
- java字符串内存分析
- 【C语言】C语言字符串函数实现(一)
- 冒泡排序可以排序多个字符串
- JS核心系列:浅谈 call apply 与 bind
- array_multisort—对多个数组或多维数组进行排序
- 扩展系统功能——装饰模式(二):装饰模式概述
- linux centos 安装mediawiki
- Unity3D屏幕特效合成
- 最近项目中用到的js
- Java’s Architecture - 《Pro_Java_8_Programming_(3rd_edition)》