【问题标题】:Blackberry - draw label in the center of custom ButtonField?Blackberry - 在自定义 ButtonField 的中心绘制标签?
【发布时间】:2009-10-27 22:43:06
【问题描述】:

我在黑莓中创建了一个 CustomButtonField,使用它我可以设置我们自己的按钮的高度和宽度。我面临的问题是我不知道如何在按钮的中心显示标签。

【问题讨论】:

    标签: user-interface blackberry custom-controls


    【解决方案1】:

    您可以使用paint方法修改标签布局、颜色和字体。
    alt text http://img9.imageshack.us/img9/8017/custombutton.jpg
    见例子:

    class CustomButton extends ButtonField {
        int mHeight;
        int mWidth;
    
        public CustomButton(int height, int width, String label) {
            super(label, CONSUME_CLICK);
            mHeight = height;
            mWidth = width;
        }
    
        public int getPreferredHeight() {
            return mHeight;
        }
    
        public int getPreferredWidth() {
            return mWidth;
        }
    
        protected void layout(int width, int height) {
            super.layout(mWidth, mHeight);
            setExtent(mWidth, mHeight);
        }
    
        protected void paint(Graphics graphics) {
            graphics.setColor(Color.WHITE);
            String label = getLabel();
            int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1;
            int y = (getPreferredHeight() - getFont().getHeight()) >> 1;
            graphics.drawText(label, x, y);
        }
    }
    

    使用示例:

    class Scr extends MainScreen implements FieldChangeListener {
        CustomButton button1;
        CustomButton button2;
        CustomButton button3;
    
        public Scr() {
            add(button1 = new CustomButton(15, 60, "first"));
            button1.setChangeListener(this);
            add(button2 = new CustomButton(30, 120, "second"));
            button2.setChangeListener(this);
            add(button3 = new CustomButton(50, 200, "third"));
            button3.setChangeListener(this);
        }
    
        public void fieldChanged(Field field, int context) {
            if (field == button1)
                Dialog.inform("first");
            if (field == button2)
                Dialog.inform("second");
            if (field == button3)
                Dialog.inform("third");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 2014-01-06
      • 2023-03-27
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多