代号一 - 自定义对话框样式不起作用
Codename One - customize Dialog style not working
我想要自定义 Dialog
样式,具有另一种背景颜色和圆角边框,因为它看起来比默认的灰色矩形更好看。
通过设置 Dialog
的 Contentpane
的样式,这在一定程度上是可能的。问题是,底层的对话框样式仍然存在,其中显示了内容窗格。而且似乎 Dialog UDID 本身无法更改,"Dialog" 样式也无法在设计器中或代码中覆盖。
Form hi = new Form();
hi.getUnselectedStyle().setBgColor(0xffffff);
Button but = new Button("open dialog");
but.addActionListener(e -> {
Dialog d = new Dialog(BoxLayout.y());
d.setUIID("Container"); // this line has no effect, the outside dialog component is still visible
Style s = d.getContentPane().getUnselectedStyle();
s.setBorder(RoundRectBorder.create());
s.setBgColor(0x00ff00);
s.setBgTransparency(255);
s.setMargin(5, 5, 5, 5); // adding some margin between contentpane and Dailog container, to be more obvious
d.setDisposeWhenPointerOutOfBounds(true);
// title
Label title = new Label();
title.setText("Confirmation");
d.add(title);
// body field with spanlabel info text
SpanLabel bodyLabel = new SpanLabel("Body Text");
d.add(bodyLabel);
// delete button
Button okButton = new Button("Ok");
okButton.addActionListener(e2 -> {
d.dispose();
});
// exit button
Button exitButton = new Button("Cancel");
exitButton.addActionListener(e3 -> {
d.dispose();
});
d.add(GridLayout.encloseIn(2, okButton, exitButton));
d.show();
});
hi.add(but);
hi.show();
在上图中,最外面的深灰色是对话框外的着色区域。绿色是带有预期圆形边框的内容窗格。中间的浅灰色来自我想去掉的Dialog风格
这可以做到吗?
简答:setDialogUIID("Container");
然而,通过代码自定义对话框有点问题,我强烈建议通过 designer/css 对它们进行样式设置,因为我们只是没有将它们设计为手动样式,因此您依赖于内部实现细节那可能会坏掉。
当您在 Dialog
上调用 getContentPane()
时,您正在设置 Dialog
的内容窗格的样式。不是 Dialog
本身,因此对话框样式仍然具有非透明背景。您可以使用 getDialogStyle()
来设置 Dialog
本身的样式。我不确定效果如何。
我想要自定义 Dialog
样式,具有另一种背景颜色和圆角边框,因为它看起来比默认的灰色矩形更好看。
通过设置 Dialog
的 Contentpane
的样式,这在一定程度上是可能的。问题是,底层的对话框样式仍然存在,其中显示了内容窗格。而且似乎 Dialog UDID 本身无法更改,"Dialog" 样式也无法在设计器中或代码中覆盖。
Form hi = new Form();
hi.getUnselectedStyle().setBgColor(0xffffff);
Button but = new Button("open dialog");
but.addActionListener(e -> {
Dialog d = new Dialog(BoxLayout.y());
d.setUIID("Container"); // this line has no effect, the outside dialog component is still visible
Style s = d.getContentPane().getUnselectedStyle();
s.setBorder(RoundRectBorder.create());
s.setBgColor(0x00ff00);
s.setBgTransparency(255);
s.setMargin(5, 5, 5, 5); // adding some margin between contentpane and Dailog container, to be more obvious
d.setDisposeWhenPointerOutOfBounds(true);
// title
Label title = new Label();
title.setText("Confirmation");
d.add(title);
// body field with spanlabel info text
SpanLabel bodyLabel = new SpanLabel("Body Text");
d.add(bodyLabel);
// delete button
Button okButton = new Button("Ok");
okButton.addActionListener(e2 -> {
d.dispose();
});
// exit button
Button exitButton = new Button("Cancel");
exitButton.addActionListener(e3 -> {
d.dispose();
});
d.add(GridLayout.encloseIn(2, okButton, exitButton));
d.show();
});
hi.add(but);
hi.show();
在上图中,最外面的深灰色是对话框外的着色区域。绿色是带有预期圆形边框的内容窗格。中间的浅灰色来自我想去掉的Dialog风格
这可以做到吗?
简答:setDialogUIID("Container");
然而,通过代码自定义对话框有点问题,我强烈建议通过 designer/css 对它们进行样式设置,因为我们只是没有将它们设计为手动样式,因此您依赖于内部实现细节那可能会坏掉。
当您在 Dialog
上调用 getContentPane()
时,您正在设置 Dialog
的内容窗格的样式。不是 Dialog
本身,因此对话框样式仍然具有非透明背景。您可以使用 getDialogStyle()
来设置 Dialog
本身的样式。我不确定效果如何。