0

有人可以让我知道为什么我不能将 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);
4

1 回答 1

3

EditFields 默认情况下会消耗布局期间传递给它们的所有可用宽度。因此,第二个 EditField 的可用宽度为 0。为了并排布置它们,您必须:

  1. 在父母的sublayout()方法中手动布局它们(或layout()在的情况下Manager)。
  2. 覆盖 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);


结果

在此处输入图像描述

于 2012-05-20T05:45:49.327 回答