質問

次のコードで 2 つの EditField を並べて配置できない理由を誰かが教えてください。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デフォルトでは、レイアウト中に渡された利用可能な幅をすべて消費します。その結果、2 番目の 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);


結果

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top