不是听来调整的,你也可以使用具有更多列的GridLayout,让你根据需要分配空间内容跨越多列。
例如,对于70:30分布,创建一个10列的网格布局。让第一个孩子跨越7列,第二个孩子跨越3列。
根据您的使用情况,使用GridLayout的一个缺点可能是它不会强制孩子们缩小到父母的大小,如果他们想要变大一些(我经常遇到的问题是长的标签可能只是包装)。
避免这个问题的一个选项是实现支持百分比简单的布局管理器:
public class ColumnLayout extends Layout {
int[] percentages;
public ColumnLayout(int... percentages) {
this.percentages = percentages;
}
@Override
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
Control[] children = composite.getChildren();
int height = hHint;
int width = wHint;
int consumedPercent = 0;
for (int i = 0; i < children.length; i++) {
int percent;
if (i >= percentages.length) {
percent = (100 - consumedPercent)/(children.length - percentages.length);
} else {
percent = percentages[i];
consumedPercent += percent;
}
Point childSize = children[i].computeSize(wHint == -1 ? -1 : wHint * percent/100, hHint);
if (wHint == -1) {
width = Math.max(width, childSize.x * (100 - percent)/100);
}
if (hHint == -1) {
height = Math.max(height, childSize.y);
}
}
return new Point(width, Math.max(height, 0));
}
@Override
protected void layout(Composite composite, boolean flushCache) {
Control[] children = composite.getChildren();
Rectangle available = composite.getClientArea();
int x = available.x;
int consumedPercent = 0;
for (int i = 0; i < children.length - 1; i++) {
int percent;
if (i >= percentages.length) {
percent = (100 - consumedPercent)/(children.length - percentages.length);
} else {
percent = percentages[i];
consumedPercent += percent;
}
int w = available.width * percent/100;
children[i].setBounds(x, available.y, w, available.height);
x += w;
}
if (children.length > 0) {
children[children.length - 1].setBounds(x, available.y,
available.width - (x - available.x), available.height);
}
}
}
这就是说,我现在通常只是扣帽子我想包装只是一个额外的“笼子”复合材料,其限制了他们的水平尺寸要求
对答案有任何反馈? – Baz
@Baz还没有时间去测试它。不久。尽管我对你有完全的信心,但是,巴兹=) –
够公平的,慢慢来。我只是想查看我给出的答案,看看他们是否能解决问题。 – Baz