0

我开发了一个 GWT 应用程序,它最初只适用于台式电脑浏览器。现在我决定将它也提供给智能手机和平板电脑。我为每个用户代理创建了一个不同的 .css。现在我的问题是,如何根据用户代理的类型决定加载哪些文件?这个策略是一个好策略,还是有更好的做法?

4

2 回答 2

2

要根据用户代理交换实现,您可以使用延迟绑定,这是 GWT 的内置功能。

在你的 module.gwt.xml 中输入如下内容:

<module>
   ...
  <inherits name='com.google.gwt.sample.mobilewebapp.FormFactor'/>
  ...
  <!-- Use ClientFactoryImpl by default -->
  <replace-with class="com.google.gwt.sample.mobilewebapp.client.ClientFactoryImpl">
    <when-type-is class="com.google.gwt.sample.mobilewebapp.client.ClientFactory"/>
  </replace-with>

  <!-- Use ClientFactoryImplMobile for mobile form factor. -->
  <replace-with class="com.google.gwt.sample.mobilewebapp.client.ClientFactoryImplMobile">
    <when-type-is class="com.google.gwt.sample.mobilewebapp.client.ClientFactory"/>
    <when-property-is name="formfactor" value="mobile"/>
  </replace-with>

  <!-- Use ClientFactoryImplTablet for tablet form factor. -->
  <replace-with class="com.google.gwt.sample.mobilewebapp.client.ClientFactoryImplTablet">
    <when-type-is class="com.google.gwt.sample.mobilewebapp.client.ClientFactory"/>
    <when-property-is name="formfactor" value="tablet"/>
  </replace-with>

</module>

然后只需调用 GWT.create(ClientFactory.class) 即可在运行时获得正确的实现。对于 CSS,使用 CSSResource 或 ClientBundle 的子类。来源在这里。

于 2013-09-12T07:44:14.260 回答
1

我正在使用mgwt创建移动 gwt 应用程序。

有一个基于用户代理的主题:https ://code.google.com/p/mgwt/source/browse/src/main/java/com/googlecode/mgwt/ui/client/theme/MGWTThemeBaseThemeStandardImpl.java

在您的 Bundle 中,您可以分离不同的 css。如果你不使用 CssResources 你可以只使用StyleInjector

于 2013-09-11T20:13:15.880 回答