BlackBerry:将FieldManager置于屏幕中央

时间:2012-12-27 04:38:22

标签: blackberry horizontalfieldmanager fieldmanager

我知道如何将字段水平和垂直放在屏幕中央。

然而我在这个领域取得了成功。但我想将VerticalyFieldManager或Horizo​​ntalFieldManage设置为屏幕的中心。

我的代码如下,但我无法将其设置在屏幕中央。

    final Bitmap scale = new Bitmap(Display.getWidth(),Display.getHeight());
    _backgroundBitmap.scaleInto(scale, Bitmap.FILTER_LANCZOS);

    //==============================
    // this manager is used for the static background image
    mainManager = new VerticalFieldManager(Manager.USE_ALL_HEIGHT | Manager.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL) {
        public void paint(Graphics graphics) {
            graphics.clear();
            graphics.drawBitmap(0, 0, deviceWidth, deviceHeight,scale, 0, 0);
            super.paint(graphics);
        }
    };


    // this manger is used for adding the componentes
    subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) {

        /*protected void sublayout(int maxWidth, int maxHeight) {
            int displayWidth = deviceWidth;
            int displayHeight = deviceHeight;
            super.sublayout(displayWidth, displayHeight);
            setExtent(displayWidth, displayHeight);
        }*/
    };

    VerticalFieldManager dataManager = new VerticalFieldManager(){};

    dataManager.setMargin(20, 20, 20, 20);
    //CategoryListLayout 
    //===============================================================================
    //====================================
    HorizontalFieldManager categoryLayout = new HorizontalFieldManager(FIELD_VCENTER | USE_ALL_WIDTH | FIELD_HCENTER){
        protected void paint(Graphics graphics) {
            graphics.setColor(0xFFFFFFFF);
            super.paint(graphics);
        }
    };

    LabelField cateName = new LabelField("Category", FIELD_VCENTER){
        protected void layout(int width, int height) {
            super.layout(width, height);
            this.setExtent(150, this.getHeight());
        }
    };
    categoryLayout.setBorder(myBorder);
    //choice_country.setMinimalWidth(30);
    choice_country.setMargin(10, 10, 10, 10);
    categoryLayout.add(cateName);
    categoryLayout.add(new BitmapField(Bitmap.getBitmapResource("vertical_line.png"), FIELD_VCENTER));
    categoryLayout.add(choice_country);
    dataManager.add(categoryLayout);
    //===============================================================================


    //DistanceListLayout 
    //===============================================================================
    HorizontalFieldManager distanceLayout = new HorizontalFieldManager(FIELD_VCENTER |  USE_ALL_WIDTH | FIELD_HCENTER){
        protected void paint(Graphics graphics) {
            graphics.setColor(0xFFFFFFFF);
            super.paint(graphics);
        }
    };

    LabelField distName = new LabelField("Distance", FIELD_VCENTER){
        protected void layout(int width, int height) {
            super.layout(width, height);
            this.setExtent(150, this.getHeight());
        }
    };
    distanceLayout.setBorder(myBorder);
    //choice_distance.setMinimalWidth(300);
    choice_distance.setMargin(10, 10, 10, 10);
    distanceLayout.add(distName);
    distanceLayout.add(new BitmapField(Bitmap.getBitmapResource("vertical_line.png"), FIELD_VCENTER));
    distanceLayout.add(choice_distance);
    dataManager.add(distanceLayout);
    //===============================================================================

    listNames = new Vector();
    listImage = new Vector();
    //new GetList().execute(null);

    ButtonField b_search = new ButtonField("Search", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);


    b_search.setMargin(10,10,10,10);

    b_search.setChangeListener(new FieldChangeListener() {
        public void fieldChanged(Field field, int context) {
            if (choice_country.getSelectedIndex() != 0
                    || choice_distance.getSelectedIndex() != 0) {
                new SubmitSearch().execute(null);
            } else {
                Dialog.alert("Select atleast One of Two(Category/Distance).");
            }
        }
    });


    dataManager.add(b_search);
    removeAllScreen();
    subManager.add(dataManager);

    mainManager.add(subManager);
    add(mainManager);

我的代码有什么问题?请帮帮我。 提前谢谢。

更新

这里我使用mainManager来显示应用程序的背景。 subManager仅适用于容器。 datamanager包含mane Horizo​​ntalFieldManager。

现在我想要的是数据管理器将在屏幕中心垂直显示。它不依赖于我要添加的Horizo​​ntalLayout。

希望你现在得到。请帮帮我。

2 个答案:

答案 0 :(得分:1)

VerticalManager会忽略任何垂直修饰符,例如FIELD_VCENTER

您必须实施自己的Manager,这会将submanager置于屏幕的中心。像这样:

protected void sublayout(int maxWidth, int maxHeight) {
        submanager.sublayout(displayWidth, displayHeight);
        //after sublayout submanager knows his real dimensions
        int submanagerWidth = submanager.getWidth();
        int submanagerHeight = submanager.getHeight();
        int x = (displayWidth - submanagerWidth) >> 1;
        int y = (displayHeight - submanagerHeight) >> 1;
        setPositionChild(submanager, x, y);
        setExtent(displayWidth, displayHeight);
}

在示例中,我假设submanager只是一个孩子。但是有几个很清楚如何修改示例。

更新

如果有几个孩子(例如2):

protected void sublayout(int maxWidth, int maxHeight) {
        int  height = 0;
        for (int i = 0; i < 2; i++) {
            submanager[i].sublayout(displayWidth, displayHeight);
            height += submanager[i].getHeight(); 
        }

        int y = (displayHeight - height) >> 1;
        if (y < 0)
            y = 0;
        for (int i = 0; i < 2; i++) {
            int submanagerWidth = submanager[i].getWidth();
            int submanagerHeight = submanager[i].getHeight();
            int x = (displayWidth - submanagerWidth) >> 1;
            setPositionChild(submanager[i], x, y);
            y += submanagerHeight;
        }
        setExtent(displayWidth, max(height, submanagerHeight));
}

答案 1 :(得分:0)

你为什么不使用这样的东西?

mainManager = new VerticalFieldManager(Manager.USE_ALL_HEIGHT | 
                                       Manager.USE_ALL_WIDTH |
                                       Manager.VERTICAL_SCROLL | 
                                       DrawStyle.HCENTER /* or DrawStyle.VCENTER */)