概览
许多应用在平板上显示的布局与手机不同。根据屏幕尺寸猜测设备类别并不可靠:结果可能随屏幕方向、显示缩放或窗口大小而变化。
KlugDeviceType 询问的是操作系统本身。在 Android 上,它使用设备的最小屏幕宽度(以密度无关像素计,600 dp 或更大即为平板),这与 Android 自身布局所用的规则相同。在 iOS 上,它使用系统的设备 idiom,可区分 iPad 与 iPhone。
无论竖屏还是横屏、无论您的应用如何缩放内容,答案都相同,并且从应用的第一帧起即可获得。
特性
- 自成一体:一个 ANE,无需添加核心库或依赖 ANE
- 手机还是平板,由操作系统给出答案
- 不受屏幕方向和应用缩放影响,保持稳定
- 以 dp 为单位的最小屏幕宽度(iOS 上为 point),供您设定自己的阈值
- 在应用启动时即可获得,早于首次布局
- 无需权限
支持的平台
Android arm64 · armv7
iOS arm64
在其他平台上,isSupported 返回 false,deviceType 为空字符串。
入门
将扩展添加到您的应用描述文件中:
<extensions>
<extensionID>com.klug.devicetype</extensionID>
</extensions>
代码示例
三个完整流程。这些 getter 立即返回,无需权限;每次读取都会重新向操作系统查询。完整文档 →
1 · 在启动时选择布局
import com.klug.devicetype.DeviceType; var type:DeviceType = DeviceType.getInstance(); // the same answer in portrait and landscape, available before the first layout if ( !type.isSupported ) useDesktopLayout(); // other platforms: deviceType is "" else if ( type.isTablet ) useTabletLayout(); // Android: 600 dp or more · iOS: iPad else usePhoneLayout();
2 · 您自己的断点
// dp on Android, points on iOS; -1 where not available var sw:int = type.smallestWidthDp; var columns:int; if ( sw >= 720 ) columns = 3; // large tablet else if ( sw >= 600 ) columns = 2; // small tablet else columns = 1; // phone, or -1: not available buildGrid( columns );
3 · 在折叠屏设备展开或合上时跟随变化
import flash.events.Event; var current:String = type.deviceType; // every read asks the system again, nothing is cached stage.addEventListener( Event.RESIZE, function( e:Event ):void { var now:String = type.deviceType; if ( now == current ) return; // a rotation alone never changes it current = now; rebuildLayout( now ); // Android: opening a foldable can turn "phone" into "tablet" }); // iOS: isTablet follows the device (iPad) and does not change; // smallestWidthDp follows the screen the app is shown on
API 概览
| DeviceType.getInstance() | 返回唯一实例。 |
| isSupported | 在 Android 和 iOS 上为 True。 |
| isTablet | 在平板上为 True:Android 为 600 dp 或更大,iOS 为 iPad;在其他平台为 false。 |
| isPhone | 在手机上为 True;在其他平台为 false。 |
| deviceType | "tablet" 或 "phone";在其他平台为空字符串。 |
| smallestWidthDp | 较小的屏幕尺寸:Android 上为 dp,iOS 上为 point;不可用时为 -1。 |
购买 KlugDeviceType
一个 AIR 应用的年度许可,含 12 个月内的所有更新。付款后,您将通过电子邮件收到扩展、ActionScript 库和文档,通常在一个工作日内。许可与条款
税费在结账时由 Link (Stripe) 计算。您需要在结账时输入 AIR 应用的应用 ID。
要求
| AIR SDK | 51.x (HARMAN) |
| iOS | iOS 17 或更高版本 |
| Android | Android 6.0 (API 23) 或更高版本 |
| 权限 | 无 |