GWT - 如何编译移动排列

时间:2011-08-12 09:25:26

标签: gwt mobile

我知道如何使用延迟绑定为不同的用户代理编译GWT应用程序,但这似乎没有提供在桌面+移动浏览器之间进行distingiush的方法。

除了制作基于gwt-mobile-webkit的新应用之外,您如何将现有的GWT应用转换为重新设计的移动界面?

2 个答案:

答案 0 :(得分:3)

如果您使用描述here的MVP模式,您可以根据用户代理切换视图的实现。

您可以拥有ClientFactoryImpl和ClientFactoryMobileImpl。然后使用GWT.create(ClientFactory.class)创建定义到.gwt.xml文件中的实现。

以下是.gwt.xml文件的示例

<replace-with class="com.bell.cts.e911.ers.web.client.ClientFactoryImpl">
  <when-type-is class="com.bell.cts.e911.ers.web.client.ClientFactory" />
  <when-property-is name="user.agent" value="ie6" />
</replace-with>

<replace-with class="com.bell.cts.e911.ers.web.client.ClientFactoryMobileImpl">
  <when-type-is class="com.bell.cts.e911.ers.web.client.ClientFactory" />
  <when-property-is name="user.agent" value="mobilesafari" />
</replace-with>

您始终可以使用此处描述的技术设置user.agents:http://code.google.com/p/google-web-toolkit/wiki/ConditionalProperties

http://jectbd.com/?p=1282

答案 1 :(得分:2)

您可以从GWT看到此示例应用程序: http://code.google.com/p/google-web-toolkit/source/browse/trunk/samples/mobilewebapp/src/com/google/gwt/sample/mobilewebapp/?r=10041 它在'FormFactor.gwt.xml'模块中检测形状因子,可能如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<!-- Defines the formfactor property and its provider function. -->
<module>

  <!-- Determine if we are in a mobile browser. -->
  <define-property name="formfactor" values="desktop,tablet,mobile"/>

  <property-provider name="formfactor">
  <![CDATA[
      // Look for the formfactor as a url argument.
      var args = location.search;
      var start = args.indexOf("formfactor");
      if (start >= 0) {
        var value = args.substring(start);
        var begin = value.indexOf("=") + 1;
        var end = value.indexOf("&");
        if (end == -1) {
          end = value.length;
        }
        return value.substring(begin, end);
      }

      // Detect form factor from user agent.
      var ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf("iphone") != -1 || ua.indexOf("ipod") != -1) {
        // iphone and ipod.
        return "mobile";
      } else if (ua.indexOf("ipad") != -1) {
        // ipad.
        return "tablet";
      } else if (ua.indexOf("android") != -1 || ua.indexOf("mobile") != -1) {
        /*
         * Android - determine the form factor of android devices based on the diagonal screen
         * size. Anything under six inches is a phone, anything over six inches is a tablet.
         */
        var dpi = 160;
        var width = $wnd.screen.width / dpi;
        var height = $wnd.screen.height / dpi;
        var size = Math.sqrt(width*width + height*height);
        return (size < 6) ? "mobile" : "tablet";
      }

      // Everything else is a desktop.
      return "desktop";
  ]]>
  </property-provider>

</module>