有人可以让我知道为什么我不能与以下代码并排放置2个EditFields吗?我可以并排放置3个按钮,但是由于某种原因,我无法让EditFields为我工作。任何帮助将不胜感激。

            //Bin Height
    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);
    EditField txtRadiusInches = new EditField("Inches: ", "", 2, BasicEditField.FILTER_NUMERIC);

    hManagerBinHeight.add(lblRadiusOfBin);
    hManagerBinHeight.add(txtRadiusFeet);
    hManagerBinHeight.add(txtRadiusInches);

    add(hManagerBinHeight);

最佳答案

默认情况下,EditField使用所有在布局期间传递给它们的可用宽度。结果,第二个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);



结果

09-08 03:43