GWT:更改树行的填充?

时间:2010-03-22 13:15:05

标签: java css gwt tree

GWT树看起来大致如下:

<div class="gwt-Tree">
    <div style="padding-top: 3px; padding-right: 3px;
                padding-bottom: 3px; margin-left: 0px;
                padding-left: 23px;">
         <div style="display:inline;" class="gwt-TreeItem">
              <table>
                   ...
              </table>
         </div>
    </div>
    <div ...>
    </div>
    ...
</div> 

我的问题是:我应该如何更改单个树行的填充?我想我可以为.gwt-Tree > div设置CSS规则做一些事情,但这看起来很糟糕。有更优雅的方式吗?


解决方案:显然没有更优雅的方式。以下是我们的所作所为,FWIW:

 .gwt-Tree > div:first-child { 
      width: 0px !important; 
      height: 0px !important; 
      margin: 0px !important; 
      padding: 0px !important; 
 } 

 .gwt-Tree > div {
      padding: 0px 5px !important;
 }

 .gwt-Tree > div[hidefocus=true] {
      width: 0px !important;
      height: 0px !important;

      margin: 0px !important;
      padding: 0px !important;
 }

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题并尝试用CSS解决它,但没有运气。 最后,我设法改变了java代码中的顶部和底部填充:

/**
    * handle top and bottom padding issue of leafs the grandparent DOM element (div) has disturbing top and bottom
    * padding of 3px, which is changed here to 0px
    * 
    * @param uiObject
    *           uiObject which is a leaf in the tree
    */
   private static void correctStyle(final UIObject uiObject) {
      if (uiObject instanceof TreeItem) {
         if (uiObject != null && uiObject.getElement() != null) {
            Element element = uiObject.getElement();
            element.getStyle().setPaddingBottom(0, Unit.PX);
            element.getStyle().setPaddingTop(0, Unit.PX);
         }
      } else {
         if (uiObject != null && uiObject.getElement() != null && uiObject.getElement().getParentElement() != null
               && uiObject.getElement().getParentElement().getParentElement() != null
               && uiObject.getElement().getParentElement().getParentElement().getStyle() != null) {
            Element element = uiObject.getElement().getParentElement().getParentElement();
            element.getStyle().setPaddingBottom(0, Unit.PX);
            element.getStyle().setPaddingTop(0, Unit.PX);
         }
      }
   }

我在代码中使用这样的方法:

Anchor fileDownloadLink = new Anchor();
fileDownloadLink.setStyleName(ViewConstants.STYLE_LINK_FILE_DOWNLOAD);
fileDownloadLink.setText(subSubStructure.getName());
fileDownloadLink.setHref(ViewConstants.HREF_DOWNLOAD_FILE_EXTRACTED_DATA + subSubStructure.getPath());
treeItem.addItem(fileDownloadLink);
StyleCorrector.correctStyle(fileDownloadLink);

答案 1 :(得分:2)

你可以这样做:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <style type="text/css">
    .gwt-Tree {background:green;}
    .gwt-Tree div {padding-top: 3px; padding-right: 3px;padding-bottom: 3px; margin-left: 0px;padding-left: 23px;background:gray;}
    .gwt-Tree div .gwt-TreeItem {padding:0;margin:0;background:red;color:#fff;}
    </style> 
</head>
<body>
<div class="gwt-Tree">
    <div>
         <div class="gwt-TreeItem">
            random
         </div>
    </div>
    <div>
         <div class="gwt-TreeItem">
            random
         </div>
    </div>
</div> 
</body>
</html>

请注意.gwt-Tree div将影响所有子div,因此您必须将它们重置为所需的样式.gwt-Tree div .gwtTreeItem。

关于“hacky&gt;”你说 - &gt;除IE6之外,所有浏览器都支持选择器。

答案 2 :(得分:0)

我看了一下GWT Tree Documentation。树和树项的CSS样式规则是

  

CSS样式规则

     

<强> .gwt树
  树本身

     

.gwt-Tree .gwt-TreeItem
  树项目

     

.gwt-Tree .gwt-TreeItem-selected
  选定的树项目

您可能希望将填充添加到 .gwt-Tree .gwt-TreeItem .gwt-Tree .gwt-TreeItem-selected