gwt - 如何跨 GWT 小部件共享样式表类型

标签 gwt stylesheet widget share

我正在尝试创建一个复合小部件,它将显示子小部件的“列表”,但我在让 CSS 在此设置中工作时遇到了一些问题。构建了一个简单的演示,试图找出解决方案。曾尝试像这样指定父小部件

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
   xmlns:g='urn:import:com.google.gwt.user.client.ui'>

 <ui:with field='res' type='egov.widgets.discussion.client.poc.Resources' />

 <g:FlowPanel ui:field="childPanel" stylePrimaryName='{res.style.parentStyle}'>

 </g:FlowPanel>

</ui:UiBinder>

代码

public class CssParent extends Composite{

interface MyUiBinder extends UiBinder<Widget, CssParent> {}
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

@UiField FlowPanel childPanel;

   public CssParent() {
      initWidget(uiBinder.createAndBindUi(this));

       CssChild child = new CssChild();
      childPanel.add(child);
   }  
}

子部件简单地指定为

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
   xmlns:g='urn:import:com.google.gwt.user.client.ui'>

 <ui:with field='res' type='egov.widgets.discussion.client.poc.Resources' />

    <g:HTMLPanel stylePrimaryName='{res.style.childStyle}'>
       Howdy folks
    </g:HTMLPanel>

  </ui:UiBinder>

现在,如您所见,它们都引用了一个资源包:

public interface Resources extends ClientBundle {

@Source("CssDemo.css")
Style style();

  public interface Style extends CssResource{
     String parentStyle();
     String childStyle();
  }
}

再次引用 CssDemo.css: .parentStyle{ 背景色:灰色;

.parentStyle .childStyle{
    background-color: yellow;
}

但由于某些原因,这些 css 规则从未应用于上面的两个 div 元素。关于为什么失败的任何想法?或者当样式包含不同 css 类之间的依赖规则时,在小部件之间共享 .css 样式表的更好方法?

最佳答案

发现需要注入(inject)这样使用的样式

 static{
     Resources.INSTANCE.style().ensureInjected();
 }

(将这个单例实例字段添加到包中)

public interface Resources extends ClientBundle {

    public static final Resources INSTANCE =  GWT.create(Resources.class);

    @Source("Resources.css")
    Style style();

它现在可以工作了,但我真的不喜欢这样做(“{res.style.someClass}”)+ 我必须在 CssResource 接口(interface)中定义的所有方法。

如果可以做一个共享的 ui:style 声明(可能基于一个 css 文件),并且像普通的内部样式 block 一样使用它,那就太好了。

关于gwt - 如何跨 GWT 小部件共享样式表类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2302779/

相关文章:

java - GWT:如何从多部分表单(文件)上传中返回(和处理)错误

java - 此 UIObject 的元素未设置;您可能错过了对 Composite.initWidget() 或 UIObject.setElement() 的调用

html - 从代码隐藏中获取背景颜色

onclick - 开关小部件 : onClick event works but how to listen when you slide the switch?

java - 在 GWT 应用程序中显示 PDF 文件

javascript - 即 10 : debug console (F12) changes behavior

html - 我如何在 Angular 中使用不同的样式表来支持不同的屏幕尺寸?

html - 如何使组合框选择类似于移动设备?

qt - 无法从 MainWindow 显示 QGridLayout 中的小部件

php - 将值从 View 文件发送到 yii 中的小部件