EditField
s 默认情况下会消耗布局期间传递给它们的所有可用宽度。因此,第二个 EditField 的可用宽度为 0。为了并排布置它们,您必须:
- 在父母的
sublayout()
方法中手动布局它们(或layout()
在的情况下Manager
)。
- 覆盖 EditField 的
layout()
方法并使其使用固定宽度而不是所有宽度。
选项 1:
HorizontalFieldManager hManagerBinHeight = new HorizontalFieldManager(FIELD_BOTTOM);
LabelField lblRadiusOfBin = new LabelField("Radius of Bin: ", LabelField.FIELD_LEFT);
final EditField txtRadiusFeet = new EditField("Feet: ", "", 3, BasicEditField.FILTER_NUMERIC);
txtRadiusFeet.setBorder(BorderFactory.createRoundedBorder(new XYEdges()));
final EditField txtRadiusInches = new EditField("Inches: ", "", 2, BasicEditField.FILTER_NUMERIC);
txtRadiusInches.setBorder(BorderFactory.createRoundedBorder(new XYEdges()));
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH) {
protected void sublayout(int maxWidth, int maxHeight) {
layoutChild(txtRadiusFeet, maxWidth/2, maxHeight);
layoutChild(txtRadiusInches, maxWidth/2, maxHeight);
setPositionChild(txtRadiusFeet, 0, 0);
setPositionChild(txtRadiusInches, txtRadiusFeet.getWidth(), 0);
setExtent(maxWidth, txtRadiusFeet.getHeight());
};
};
hfm.add(txtRadiusFeet);
hfm.add(txtRadiusInches);
hManagerBinHeight.add(lblRadiusOfBin);
hManagerBinHeight.add(hfm);
add(hManagerBinHeight);
选项 2:
HorizontalFieldManager hManagerBinHeight = new HorizontalFieldManager(FIELD_BOTTOM);
LabelField lblRadiusOfBin = new LabelField("Radius of Bin: ", LabelField.FIELD_LEFT);
EditField txtRadiusFeet = new EditField("Feet: ", "", 3, BasicEditField.FILTER_NUMERIC) {
// Limit the width of the edit field to be the half of the available width
protected void layout(int width, int height) {
super.layout(width/2, height);
}
};
txtRadiusFeet.setBorder(BorderFactory.createRoundedBorder(new XYEdges()));
EditField txtRadiusInches = new EditField("Inches: ", "", 2, BasicEditField.FILTER_NUMERIC);
txtRadiusInches.setBorder(BorderFactory.createRoundedBorder(new XYEdges()));
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
hfm.add(txtRadiusFeet);
hfm.add(txtRadiusInches);
hManagerBinHeight.add(lblRadiusOfBin);
hManagerBinHeight.add(hfm);
add(hManagerBinHeight);
结果