我想在 GWT CssResource 中将一些颜色定义为常量,并在整个应用程序中使用这些常量;但我不知道该怎么做。
我会告诉你我已经尝试过什么。我创建了一个 ClientBundle 和一个 CssResource,如下所示:
public interface Resources extends ClientBundle {
public interface MyStyle extends CssResource {
String JUNGLEGREEN();
String example();
...
}
@Source("Resources.css")
MyStyle css();
}
我定义了一些常量 http://code.google.com/webtoolkit/doc/latest/DevGuideClientBundle.html#Constants在资源.css 中:
@def JUNGLEGREEN #1F3D0A;
在 Resources.css 中,我使用这些常量,如下所示:
.example { color:JUNGLEGREEN; }
我不知道如何在其他 CSS 文件和 UiBinder 模板中重用这些常量。我想在其他一些 UiBinder 文件中执行此操作,例如 LoginView.ui.xml:
<ui:with field='resources' type='com.example.Resources' />
<ui:style>
.mainPanel {
background:{resources.css.JUNGLEGREEN};
...
}
</ui:style>
...但它似乎无法编译。你知道我怎样才能实现我的目标吗?
这是我们的做法:
- 我们把所有的常量属性放在一个
constant.css
file
@def black #241b15; /* text color */
@def orange #ff4f00; /* links */
- 在每个 ui.xml 文件中,您可以通过以下方式引用这些常量:
<ui:style src="../../resources/css/constants.css">
.myStyle {
color: orange;
}
</ui:style>
希望有帮助。
EDIT:
为了避免相对路径<ui:style>
元素,您可以执行以下操作:
- 在 css 文件中再次定义常量(例如
constants.css
)
@def junglegreen #1f3d0a;
- 创建一个
ClientBundle
and CssResource
检索定义的常量
public interface MyResources extends ClientBundle {
public static final MyResources INSTANCE = GWT.create(MyResources.class);
public interface Constants extends CssResource {
String junglegreen();
}
Constants constants();
}
-使用@eval
注释来访问常量
<ui:style>
@eval green com.gwt.client.widget.test.MyResources.INSTANCE.constants().junglegreen();
.someClass {
color: green;
}
</ui:style>
我知道如何在不引用 css 文件本身的情况下处理常量的唯一方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)