【问题标题】:Blackberry - Setting LabelField background color黑莓 - 设置 LabelField 背景颜色
【发布时间】:2010-11-18 10:31:21
【问题描述】:

我想在具有爱丽丝蓝色背景的 MainScreen 上放置几个带有右对齐文本的 LabelField。不幸的是,我似乎无法弄清楚如何做到这一点。

我能做的最好的事情是在 MainScreen 上将背景设置为 Color.ALICEBLUE 并将 LabelFields 放置在屏幕上(也具有爱丽丝蓝色背景)。

    public void paint(Graphics graphics) {
        graphics.setBackgroundColor(Color.ALICEBLUE);
        graphics.clear();
        super.paint(graphics);  
    }

还有……

    LabelField display = new LabelField("", LabelField.FIELD_RIGHT){
        public void paint(Graphics graphics) {
            graphics.setColor(Color.DIMGRAY);
            graphics.setBackgroundColor(Color.ALICEBLUE);
            graphics.clear();
            super.paint(graphics);  
        }
    };

覆盖 MainScreen 绘制例程给了我爱丽丝蓝色背景,但覆盖 LabelFields 的绘制例程似乎还不够。结果是一个白色行,仅在标签文本后面有一个爱丽丝蓝色背景。添加USE_ALL_WIDTH 更正了背景问题,但我无法与USE_ALL_WIDTH 右对齐。

有人知道解决这个问题的方法吗?

【问题讨论】:

    标签: user-interface blackberry background-color


    【解决方案1】:

    使用

    new LabelField("",LabelField.USE_ALL_WIDTH | DrawStyle.RIGHT);
    

    不覆盖 LabelField 的绘制方法。

    【讨论】:

    • 效果很好!为了获得背景颜色集,我实际上还需要重写paint方法..
    【解决方案2】:

    class BGManager extends VerticalFieldManager {
        public BGManager() {
            super(USE_ALL_HEIGHT|USE_ALL_WIDTH);
        }
        public void paint(Graphics graphics)
        {
            graphics.setBackgroundColor(Color.DARKRED);
            graphics.clear();
            super.paint(graphics);
        }
    }
    

    然后在你的屏幕上使用它,添加简单的 LabelField:

    class Scr extends MainScreen {
        BGManager manager = new BGManager();
        public Scr() {
            super();
            add(manager);       
            manager.add(new LabelField("Hello!", FIELD_RIGHT));
            manager.add(new LabelField("This is a test", FIELD_RIGHT));
        }
    }
    

    在 >= 4.6 版本中,您可以使用 setBackgroud() 方法作为默认屏幕管理器:

    class Scr extends MainScreen {  
        public Scr() {
            super();
            VerticalFieldManager manager = 
                (VerticalFieldManager)getMainManager();
            manager.setBackground(
                BackgroundFactory.createSolidBackground(
                    Color.DARKRED));        
            manager.add(new LabelField("Hello!", FIELD_RIGHT));
            manager.add(new LabelField("This is a test", FIELD_RIGHT));
        }
    }
    

    BB KB DB-00131 - How to - Change the background color of a screen

    【讨论】:

    • 也很好用!另外,这种方法使我不必为我添加的每个 LableField 覆盖绘制方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2021-10-28
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多