标签字段在字体设置期间变得笨拙

时间:2012-12-19 11:27:19

标签: blackberry

我对Blackberry 5&在模拟器上6 Os 设置字体时,标签字段变得笨拙 在Blackberry 7中运行良好

这是我的示例代码

    LabelField _lblTitle3 = 
      new LabelField(offerStatus, 
                     USE_ALL_WIDTH | Field.FIELD_VCENTER |
                     LabelField.ELLIPSIS | Field.NON_FOCUSABLE) {

       protected void drawFocus(Graphics graphics, boolean on) {
       };

       protected void paintBackground(Graphics graphics) {
           String offerStatus = _offerObj.getCategoryStatus();

           int color;
           if (offerStatus.equalsIgnoreCase("Saved"))
              color = Color.BLUE;
           else if (offerStatus.equalsIgnoreCase("Accepted!"))
              color = Color.GREEN;
           else
              color = Color.BLACK;

           if (_isFocus) {
               graphics.setColor(Color.WHITE);
           } else {
               graphics.setColor(color);
           }

           super.paint(graphics);
       };
   };

   Font myFont = Font.getDefault();
   FontFamily typeface = FontFamily.forName("Times New Roman");
   int fType = Font.BOLD
   int fSize = 12
   myFont = typeface.getFont(fType, fSize);
   _lblTitle3.setFont(myFont);

图片位于

之下

See the Field overlap

1 个答案:

答案 0 :(得分:3)

你想做什么?只需更改字体颜色?

如果是这样,您可能不想覆盖paintBackground()

paintBackground()的实施中,您正在呼叫super.paint()。我不确定那会怎么做,但如果那是错误的话我也不会感到惊讶。

paint()paintBackground()是两个不同的东西。

如果您只想更改字体颜色,具体取决于文本和焦点状态,只需将该逻辑放在paint()方法中,然后单独留下paintBackground()(不要覆盖它)

此外,当您更改Graphics上下文时,要执行设置新颜色等操作,首先存储旧颜色通常更安全,并在以后重置它。像这样:

        protected void paint(Graphics graphics) {
            int oldColor = graphics.getColor();

            String offerStatus = _offerObj.getCategoryStatus();
            int color;
            if (offerStatus.equalsIgnoreCase("Saved"))
                color = Color.BLUE;
            else if (offerStatus.equalsIgnoreCase("Accepted!"))
                color = Color.GREEN;
            else
                color = Color.BLACK;

            if (_isFocus) {
                graphics.setColor(Color.WHITE);
            } else {
                graphics.setColor(color);
            }
            super.paint(graphics);

            graphics.setColor(oldColor);
        };
相关问题