为什么我的TableLayout不是我的组件的中心?我只在一个列中放置了几个组件,我希望它们居中,但TableLayout的中心约束在这种情况下似乎不起作用。
我应该提一下,我也试过了BoxLayout,我没有看到任何方法将组件集中在其中。此外,我计划在下面添加更多组件,因此无法使用BorderLayout。
这是它的外观。这符合我在Android手机上看到的内容。
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() {
}
}
答案 0 :(得分:4)
尝试添加:
layout.setGrowHorizontally(true);
首选尺寸不占据整个屏幕。