0

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

但是我在该领域取得了成功,但我想将 VerticalFieldManager 或 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。

4

2 回答 2

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));
}
于 2012-12-27T15:37:44.827 回答
0

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

mainManager = new VerticalFieldManager(Manager.USE_ALL_HEIGHT | 
                                       Manager.USE_ALL_WIDTH |
                                       Manager.VERTICAL_SCROLL | 
                                       DrawStyle.HCENTER /* or DrawStyle.VCENTER */)
于 2012-12-31T07:23:50.800 回答