为什么我的 TableLayout 不使我的组件居中?
Why doesn't my TableLayout center my components?
为什么我的 TableLayout 不能使我的组件居中?我只将几个组件放在一个列中,我希望它们居中,但 TableLayout 的中心约束在这种情况下似乎不起作用。
我应该提一下,我也尝试过 BoxLayout,但我没有看到任何方法可以将其中的组件居中。另外,我打算在下面添加更多的组件,所以 BorderLayout 是不可能的。
这是它的样子。这与我在 Android phone.
上看到的相符
public class Playground {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
}
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Table Layout Test");
TableLayout layout = new TableLayout(4, 1);
hi.setLayout(layout);
// I specify row and column explicitly or the replaceAndWait() method will put the
// replacement below the button instead of above it.
TableLayout.Constraint center;
center = layout.createConstraint(0, 0).horizontalAlign(hi.CENTER);
red = new Label(makeBlankImage(200, 200, 0xFF0000));
blue = new Label(makeBlankImage(200, 200, 0xFF));
visibleLabel = red;
hi.add(center, visibleLabel);
center = layout.createConstraint(1, 0).horizontalAlign(hi.CENTER);
Button button = new Button("Switch");
button.addActionListener(evt -> {
Label priorImage = visibleLabel;
//noinspection ObjectEquality
if (visibleLabel == red) {
visibleLabel = blue;
} else {
visibleLabel = red;
}
hi.replaceAndWait(priorImage, visibleLabel, CommonTransitions.createFade(500));
});
hi.add(center, button);
center = layout.createConstraint(2, 0).horizontalAlign(hi.CENTER);
Label label = new Label("Why aren't any of these");
hi.add(center, label);
center = layout.createConstraint(3, 0).horizontalAlign(hi.CENTER);
SpanLabel label2 = new SpanLabel("components centered?");
hi.add(center, label2);
hi.show();
current = hi;
}
private Label red;
private Label blue;
private Label visibleLabel;
private Image makeBlankImage(final int width, final int height, int color) {
Image image = Image.createImage(width, height);
Graphics graphics = image.getGraphics();
graphics.setColor(color);
graphics.fillRect(0, 0, width, height);
return image;
}
public void stop() {
}
public void destroy() {
}
}
尝试添加:
layout.setGrowHorizontally(true);
首选尺寸没有占据整个屏幕。
为什么我的 TableLayout 不能使我的组件居中?我只将几个组件放在一个列中,我希望它们居中,但 TableLayout 的中心约束在这种情况下似乎不起作用。
我应该提一下,我也尝试过 BoxLayout,但我没有看到任何方法可以将其中的组件居中。另外,我打算在下面添加更多的组件,所以 BorderLayout 是不可能的。
这是它的样子。这与我在 Android phone.
上看到的相符public class Playground {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
}
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Table Layout Test");
TableLayout layout = new TableLayout(4, 1);
hi.setLayout(layout);
// I specify row and column explicitly or the replaceAndWait() method will put the
// replacement below the button instead of above it.
TableLayout.Constraint center;
center = layout.createConstraint(0, 0).horizontalAlign(hi.CENTER);
red = new Label(makeBlankImage(200, 200, 0xFF0000));
blue = new Label(makeBlankImage(200, 200, 0xFF));
visibleLabel = red;
hi.add(center, visibleLabel);
center = layout.createConstraint(1, 0).horizontalAlign(hi.CENTER);
Button button = new Button("Switch");
button.addActionListener(evt -> {
Label priorImage = visibleLabel;
//noinspection ObjectEquality
if (visibleLabel == red) {
visibleLabel = blue;
} else {
visibleLabel = red;
}
hi.replaceAndWait(priorImage, visibleLabel, CommonTransitions.createFade(500));
});
hi.add(center, button);
center = layout.createConstraint(2, 0).horizontalAlign(hi.CENTER);
Label label = new Label("Why aren't any of these");
hi.add(center, label);
center = layout.createConstraint(3, 0).horizontalAlign(hi.CENTER);
SpanLabel label2 = new SpanLabel("components centered?");
hi.add(center, label2);
hi.show();
current = hi;
}
private Label red;
private Label blue;
private Label visibleLabel;
private Image makeBlankImage(final int width, final int height, int color) {
Image image = Image.createImage(width, height);
Graphics graphics = image.getGraphics();
graphics.setColor(color);
graphics.fillRect(0, 0, width, height);
return image;
}
public void stop() {
}
public void destroy() {
}
}
尝试添加:
layout.setGrowHorizontally(true);
首选尺寸没有占据整个屏幕。