2012-03-29 46 views
1

我正在制作BlackBerry OS 6+应用程序,我需要绘制一个特定颜色的固体正方形(在运行时给出),但它应该可以添加到VerticalFieldManager。所以我认为定制绘图使用Graphics对象不是一个选项。如何在BlackBerry中绘制一个实心方形?

我已经尝试将LabelField的背景颜色设置为我想要的颜色并将LabelField添加到VerticalFieldManager。为了获得方形外观,我试图覆盖和getPreferredHeightLabelField以返回更高的值(例如:150)。但是,虽然宽度显示正确,但无论我返回什么值,高度都保持不变。

那么有什么办法可以做到这一点?总之,我想要的是:

  • 一个坚实的方形块的颜色(颜色决定在运行时)。
  • 应将其添加到VerticalFieldManager

在此先感谢!

+0

你可以尝试这样的:http://stackoverflow.com/questions/8927472/thick-border-for-rounded-button-in-blackberry/8928025#8928025 – alishaik786 2012-03-29 11:16:07

+0

@ alishaik786不,我想要纯粹的方形的东西......它不需要任何听众或行为。仅用于显示。 – Roshnal 2012-03-29 11:25:30

回答

3

试试这段代码,在构造函数中传入颜色。

import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.Font; 
import net.rim.device.api.ui.Graphics; 

public class CustomField extends Field 
{ 

private int backgroundColour; 
private int fieldWidth; 
private int fieldHeight; 
private int padding = 8; 

public CustomField(int color) 
{ 
    super(Field.FOCUSABLE); 
    fieldHeight = 100; 
    fieldWidth = 100; 
    this.setPadding(2, 2, 2, 2); 
    this.backgroundColour=color; 
} 

public int getPreferredWidth() 
{ 
    return fieldWidth; 
} 

public int getPreferredHeight() 
{ 
    return fieldHeight; 
} 

protected void layout(int arg0, int arg1) 
{ 
    setExtent(getPreferredWidth(), getPreferredHeight()); 
} 

protected void drawFocus(Graphics graphics, boolean on) 
{ 

} 

protected void paint(Graphics graphics) 
{ 
    graphics.setColor(backgroundColour); 
    graphics.fillRect(0, 0, fieldWidth, fieldHeight); 
} 
} 
+0

很好地工作!非常感谢您的回答! – Roshnal 2012-03-29 12:38:20

0
VerticalFieldManager vfm = new VerticalFieldManager(); 

    Field f = new Field() { 

     protected void paint(Graphics graphics) { 
      graphics.setBackgroundColor(Color.RED); 
      graphics.clear(); 
      graphics.drawRect(10, 10, 100, 100); 
      graphics.fillRect(10, 10, 100, 100); 
     } 

     protected void layout(int width, int height) { 
      // TODO Auto-generated method stub 

      setExtent(200, 200); 
     } 
    }; 
    vfm.add(f); 

    add(vfm); 
相关问题