2
我发现了一些针对此问题的解决方案,但它们仅适用于RCP平台。我正在使用RAP平台,我无法找到解决方案。如何将图像与表格单元格的中心对齐(SWT表格)RAP平台
我发现了一些针对此问题的解决方案,但它们仅适用于RCP平台。我正在使用RAP平台,我无法找到解决方案。如何将图像与表格单元格的中心对齐(SWT表格)RAP平台
要自行对齐的图像,你必须在RAP两种选择:
标记
相应准备表后,您可以使用HTML的一个子集与表中的项目的文本。
Table table = new Table(parent, SWT.NONE);
table.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
table.setData(RWT.CUSTOM_ITEM_HEIGHT, Integer.valueOf(80));
TableItem item = new TableItem(table, SWT.NONE);
String imageUrl = RWT.getRequest().getContextPath() + "/" + registerImage("foo.png");
item.setText("<img src='" + imageUrl + "' width='10' height='10' style='padding-right:5px'/>");
注册一个图像的代码(这里假定图像位于类路径上)应该是这样的:
String registerImage(String resourceName) throws IOException {
IResourceManager resourceManager = RWT.getResourceManager();
if(!resourceManager.isRegistered(resourceName)) {
InputStream inputStream = CLASSLOADER.getResourceAsStream(resourceName);
try {
resourceManager.register(resourceName, inputStream);
} finally {
inputStream.close();
}
}
return resourceManager.getLocation(resourceName);
}
更多细节可以在这里找到:http://eclipsesource.com/blogs/2012/09/12/making-your-images-available-using-markup/
行模板
使用行模板,可以将表的默认列布局替换为nd项目布局的方式可以改变。
要更改为图像的位置,它将像往常一样设置为item.setImage(...)
,然后创建一个模板以根据需要定位图像。 最后,表格被告知使用table.setData(...)
的模板。
Template template = new Template();
ImageCell imageCell = new ImageCell(template);
imageCell.setBindingIndex(0); // bind to image from column 0
imageCell.setTop(4).setLeft(4).setWidth(48).setHeight(48);
table.setData(RWT.ROW_TEMPLATE, template);
更多关于此这里:http://eclipsesource.com/blogs/2013/11/14/rap-2-2m3-introducing-row-templates/
看到http://stackoverflow.com/questions/8968352/how-to-align-image-to-center-of-table-cell-swt-table – flafoux
@flafoux的建议在RAP中不起作用,因为它不支持所有者绘制的表项。 –