簡體   English   中英

如何在BlackBerry中繪制一個堅固的方塊?

[英]How can I draw a solid square in BlackBerry?

我正在制作一個BlackBerry OS 6+應用程序,我需要繪制一個特定顏色的實心方塊(在運行時給出), 但它應該可以添加到 VerticalFieldManager 所以我認為使用Graphics對象的自定義繪圖不是一個選項。

我已經嘗試將LabelField的背景顏色設置為我想要的顏色,並將LabelField添加到VerticalFieldManager 為了獲得方形外觀,我嘗試重寫LabelFieldgetPreferredWidth()getPreferredHeight以返回更高的值(例如:150)。 但是雖然正確顯示了寬度,但無論我返回什么值,高度都保持不變。

那么有什么辦法可以實現這個目標嗎? 總之,我想要的是:

  • 一個實心方形的顏色塊(顏色在運行時決定)。
  • 哪個應該添加到VerticalFieldManager

提前致謝!

試試這段代碼,傳入構造函數中的顏色。

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); 
}
}
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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM