BlackBerry - BrowserField自定义大小小于默认值

时间:2012-03-08 12:42:54

标签: blackberry

我正在与BrowserField挣扎。我想要一个顶部有BrowserField的屏幕,底部有一个屏幕。我想将每个人都指向一个包含图片(也许是HTML)的网址 - 一种人们可能会看到AdMob广告的方式。

所以我想为BrowserField定义一个自定义大小。可以这样做吗?

  • 目前,BrowserField占据了整个屏幕。 (* 见下面的更新
  • 我试图匿名继承它 - 但它是最终的。

这是一个可能的用例,还是我应该寻找一个完全不同的问题解决方案......?


更新

我已将BrowserField放在VerticalFieldManager内并相应调整大小(根据Blackberry BrowserField does not fit to screen)。

(重新编辑:这是正确的想法,但我在我的代码中犯了一个错误,让我认为它仍然被破坏。下面接受的答案提供了完美的代码来完成我所希望的。)

1 个答案:

答案 0 :(得分:2)

我已经安排了两个BrowserFields。一个与顶部对齐,另一个与底部对齐。它们的高度由TOP_BROWSER_FIELD_HEIGHTBOTTOM_BROWSER_FIELD_HEIGHT相应定义。在中间有另一个管理器消耗布局BrowserFields后剩下的所有可用高度。

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class BrowserFieldScreen extends MainScreen implements FieldChangeListener {
    private static final int TOP_BROWSER_FIELD_HEIGHT = Display.getHeight() / 3;
    private static final int BOTTOM_BROWSER_FIELD_HEIGHT = Display.getHeight() / 3;

    private BrowserField topBrowserField;
    private BrowserField bottomBrowserField;
    private VerticalFieldManager vfmCenter;
    private ButtonField startButton;

    public BrowserFieldScreen() {
        super(NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL | USE_ALL_WIDTH | USE_ALL_HEIGHT);

        final VerticalFieldManager topVfm = new VerticalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH);
        topBrowserField = new BrowserField();
        topVfm.add(topBrowserField);

        final VerticalFieldManager bottomVfm = new VerticalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH);
        bottomBrowserField = new BrowserField();
        bottomVfm.add(bottomBrowserField);

        vfmCenter = new VerticalFieldManager(USE_ALL_WIDTH);       
        vfmCenter.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
        HorizontalFieldManager hfmCenter = new HorizontalFieldManager(USE_ALL_HEIGHT | FIELD_HCENTER);
        startButton = new ButtonField("START", FIELD_VCENTER);
        startButton.setChangeListener(this);
        hfmCenter.add(startButton);
        vfmCenter.add(hfmCenter);

        VerticalFieldManager vfm = new VerticalFieldManager() {
            protected void sublayout(int maxWidth, int maxHeight) {
                setExtent(maxWidth, maxHeight);

                // layout BrowserFields first
                layoutChild(topVfm, maxWidth, TOP_BROWSER_FIELD_HEIGHT);
                layoutChild(bottomVfm, maxWidth, BOTTOM_BROWSER_FIELD_HEIGHT);

                // layout center field manager last to make it occupy all the height left
                int maxCenterHeight = maxHeight - (TOP_BROWSER_FIELD_HEIGHT + BOTTOM_BROWSER_FIELD_HEIGHT);
                layoutChild(vfmCenter, maxWidth, maxCenterHeight);

                int yPos = 0;
                setPositionChild(topVfm, 0, yPos);
                yPos += TOP_BROWSER_FIELD_HEIGHT;
                setPositionChild(vfmCenter, 0, yPos);
                yPos += vfmCenter.getHeight();
                setPositionChild(bottomVfm, 0, yPos);
            };
        };

        vfm.add(topVfm);
        vfm.add(vfmCenter);
        vfm.add(bottomVfm);
        add(vfm);
    }

    public void fieldChanged(Field field, int context) {
        if (field == startButton) {
            topBrowserField.requestContent("http://www.google.com");
            bottomBrowserField.requestContent("http://www.yahoo.com");
        }
    }
}


结果是 enter image description here