2.3 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.

虽然最新版本的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的活动设备的分布情况,具体取决于访问Google 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版本。 具体来说,<uses-sdk>元素的minSdkVersiontargetSdkVersion属性标识了您的应用程序兼容的最低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在Build常量类中为每个平台版本提供了一个唯一的代码。 在您的应用程序中使用这些代码来构建条件,以确保仅在系统上提供这些API时才执行依赖于更高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。 然后将菜单项添加到操作栏,您需要在菜单资源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提供用户体验主题,为应用程序提供底层操作系统的外观和感觉。 这些主题可以应用到清单文件中的应用程序。 通过使用这些内置的样式和主题,您的应用程序将自然地遵循Android的最新外观和每个新版本的感觉。

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:

要应用您定制在/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>元素中:

<application android:theme="@style/CustomTheme">

For more about creating and using themes, read the Styles and Themes guide.

有关创建和使用主题的更多信息,请阅读Styles and Themes指南。

转载于:https://www.cnblogs.com/youseiraws/p/7016053.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值