我开发了一个 GWT 应用程序,它最初只适用于桌面 PC 浏览器。现在我决定让智能手机和平板电脑也可以使用它。我为每个用户代理创建了不同的 .css。现在我的问题是,我如何根据用户代理的类型决定加载哪些文件?这个策略好还是有更好的做法?
最佳答案
要根据用户代理交换实现,您可以使用 deferred binding ,这是一个内置的 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 的子类。 Source is here.
关于css - 基于用户代理加载不同的 .css - GWT 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18749199/