2012-03-29 38 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); 
相關問題