我想将一些颜色定义为 GWT CssResource 中的常量,并在整个应用程序中使用这些常量;但我不知道该怎么做。
我会告诉你我尝试了什么。我创建了一个 ClientBundle 和一个 CssResource 如下:
public interface Resources extends ClientBundle {
public interface MyStyle extends CssResource {
String JUNGLEGREEN();
String example();
...
}
@Source("Resources.css")
MyStyle css();
}
我已经定义了一些 constants在 Resources.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
中文件 @def black #241b15; /* text color */
@def orange #ff4f00; /* links */
<ui:style src="../../resources/css/constants.css">
.myStyle {
color: orange;
}
</ui:style>
希望有帮助。
编辑:
避免
<ui:style>
中的相对路径元素,您可以执行以下操作:constants.css
)@def junglegreen #1f3d0a;
ClientBundle
和 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 文件本身的情况下处理常量的唯一方法。
关于gwt - 在 GWT 中需要应用范围的 CSS 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3533211/