【问题标题】:Titanium: platform detection and changing interface style accordinglyTitanium:平台检测并相应更改界面样式
【发布时间】:2012-07-28 03:12:03
【问题描述】:

您可能知道,不同的平台需要稍微不同的 UX/UI。

例如,当您为 iphone 设计时,您可能有一个后退按钮,但是当您为 android 构建时,您不需要后退按钮。

其他的东西是图标,你可能在 android 的工具栏上有多个按钮,而在 iphone 的工具栏上只有 2 个按钮。

所以问题是...当您构建 js 文件来定义界面时,您是构建两个不同的界面 js 文件,每个特定于平台,还是只构建 1 个会根据平台检测更改 UI 的 js 文件。


我认为有两套特定于平台的 UI 可能更容易,而不是在平台检测上改变样式,因为 UX 甚至可能不同,所以 UX 和 UI 的代码会相当复杂?你觉得呢?

【问题讨论】:

    标签: javascript android iphone titanium titanium-mobile


    【解决方案1】:

    我认为,拥有两组特定于平台的 UI 是更好的选择。 示例应用程序(内置于钛工作室)展示了如何决定平台。以下是示例应用程序的代码:

    var osname = Ti.Platform.osname,
        version = Ti.Platform.version,
        height = Ti.Platform.displayCaps.platformHeight,
        width = Ti.Platform.displayCaps.platformWidth;
    
    //considering tablet to have one dimension over 900px - this is imperfect, so you should feel free to decide
    //yourself what you consider a tablet form factor for android
    var isTablet = osname === 'ipad' || (osname === 'android' && (width > 899 || height > 899));
    
    var Window;
    if (isTablet) {
            Window = require('ui/tablet/ApplicationWindow');
    }
    else {
        // Android uses platform-specific properties to create windows.
        // All other platforms follow a similar UI pattern.
        if (osname === 'android') {
            Window = require('ui/handheld/android/ApplicationWindow');
        }
        else {
            Window = require('ui/handheld/ApplicationWindow');
        }
    }
    new Window().open();
    

    【讨论】:

    • 最好有一个应用级函数和一个应用级变量来存储平台和版本,因为每次调用Ti.Platform.osname 时,Ti 都必须进入操作系统。此外,您不必一直使用osname === 'android',而是使用if (app.isAndroid()) {}
    • @Cyntech,这个 app.isAndroid 的“应用”在哪里定义?
    • @TonyAdams 我使用全局应用程序变量(在我的 bootsrap js 文件中定义)来存储顶级配置变量或函数,甚至是窗口。但是,自从 Alloy 出现以来,我还没有编写 Ti 代码,所以做这种事情的公认方法可能已经改变了。
    • @TonyAdams,您可以使用 Ti.App.variableName 来制作全局变量。因此,例如,在上述情况下: Ti.App.isTablet = (osname === 'ipad' || (osname === 'android' && (width > 899 || height > 899))) ;
    • @DhairyaVora,Cyntech,谢谢!这是非常好的知识!
    【解决方案2】:

    最好将您的业务逻辑和 UI .js 文件分开。还要为每个平台创建一个.js 文件,您可以根据平台指定正确的js URL。您可以参考 Kitchen Sink 选项卡示例以获得清晰的思路。

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      相关资源
      最近更新 更多