【问题标题】:Put FieldManager at center of the screen in a BlackBerry app将 FieldManager 置于 BlackBerry 应用程序的屏幕中心
【发布时间】:2012-12-12 12:47:20
【问题描述】:

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

但是我在该领域取得了成功,但我想将 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 将包含鬃毛 Horizo​​ntalFieldManager。

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

【问题讨论】:

  • 你能给出你想要得到的小样机图片吗?
  • 哪个经理你想集中,那不是集中?
  • @EugenMartynov 我已经更新了这个问题,请参考它,让我帮忙。
  • @Nate:我已经更新了问题。

标签: blackberry horizontalfieldmanager fieldmanager


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    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));
    }
    

    【讨论】:

    • 感谢您的回答。因此,如果有多个孩子,那么在每个孩子中我都必须放置这个子布局?
    • 如果有更多的孩子,你必须总结他们的身高。相应地计算 y 位置后
    • submanager.sublyout(displayWidth, displayHeight) 出现语法错误
    • 没错,你需要引入这个变量。关注Display.getWidth()
    • 我知道 Display.getWidth()。但我在 .sublayout 出现错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多