首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在BlackBerry应用程序中将FieldManager置于屏幕中央

在BlackBerry应用程序中将FieldManager置于屏幕中央
EN

Stack Overflow用户
提问于 2012-12-27 12:38:22
回答 2查看 214关注 0票数 0

我知道如何在屏幕中央放置水平和垂直的领域。

然而,我在该字段中获得了成功,但我希望将VerticalFieldManager或HorizontalFieldManage设置为屏幕的中心。

我的代码如下所示,但是我不能将它设置在屏幕的中心。

代码语言:javascript
复制
    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只适用于容器。数据管理器包括mane HorizontalFieldManager。

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

EN

回答 2

Stack Overflow用户

发布于 2012-12-27 23:37:45

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

你必须实现你自己的Manager,它会把submanager放在屏幕的中心。如下所示:

代码语言:javascript
复制
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):

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-12-31 15:23:51

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

代码语言:javascript
复制
mainManager = new VerticalFieldManager(Manager.USE_ALL_HEIGHT | 
                                       Manager.USE_ALL_WIDTH |
                                       Manager.VERTICAL_SCROLL | 
                                       DrawStyle.HCENTER /* or DrawStyle.VCENTER */)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14049418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档