黑莓 - 田野 - 水平对齐

时间:2009-07-07 16:19:31

标签: java blackberry user-interface

我想将自定义按钮字段(屏幕截图上的星号)与水平管理器中的中心对齐

alt text

HFM的定义

final HorizontalFieldManager _navigation = new HorizontalFieldManager(HorizontalFieldManager.NO_VERTICAL_SCROLL | HorizontalFieldManager.FIELD_VCENTER | Field.USE_ALL_WIDTH)
{    
 protected void paint(Graphics graphics)
 {
     graphics.setGlobalAlpha(100);

     int[] xInds = new int[]{0, getExtent().width, getExtent().width, 0};
     int[] yInds = new int[]{0, 0, getExtent().height, getExtent().height};
     final int[] cols = new int[]{Color.WHITE, Color.WHITE, color_computacenter_light_blue, color_computacenter_light_blue};

     graphics.drawShadedFilledPath(xInds, yInds, null, cols, null);
     super.paint(graphics);
 }
 protected void sublayout(int maxWidth, int maxHeight)
    {
        super.sublayout(maxWidth, maxHeight);
        setExtent(maxWidth, navigation_vertical_manager_height);
    }
};

自定义字段(部分)

public class NavigationButton extends Field
{
    private String label;
    Bitmap img;
    int horizontalPadding = 4;
    int verticalPadding = 10;
    static int height;
    static int weight;
    ToolTip _tooltip;

    public NavigationButton(String name, Bitmap img) 
    {
        label = name;
        this.img = img;
        this.height = img.getHeight() + 4;
        this.weight = img.getWidth()  + 2;
    }

    public NavigationButton(String name, Bitmap img, long style) 
    {
        super(style);
        label = name;
        this.img = img;
        this.height = img.getHeight() + 4;
        this.weight = img.getWidth()  + 2;
    }

    protected void layout(int maxWidth, int maxHeight) 
    {  
        setExtent(maxWidth, maxHeight);
        // setExtent(Math.min(weight, maxWidth), Math.min(height, maxHeight));
    }

    protected void paint(Graphics graphics) 
    {
        // Draw background  
        graphics.setGlobalAlpha(255);
        if(isFocus())
        {   
            graphics.setColor(0x005AA1);
            graphics.drawRoundRect(0 ,0 , weight + 1, height + 1, 2, 2);
        }
        if (img != null) 
        {
            graphics.drawBitmap(0, 0 , img.getWidth(), img.getHeight(), img, 0, 0);
        }
    }

[...]

任何想法,我做错了什么?!

谢谢!

2 个答案:

答案 0 :(得分:4)

我没有测试过这段代码,但我怀疑你的问题出在你所在领域的布局方法中:

protected void layout(int maxWidth, int maxHeight) 
    {  
        setExtent(maxWidth, maxHeight);
        // setExtent(Math.min(weight, maxWidth), Math.min(height, maxHeight));
    }

你有没有理由评论这条线?我认为发生的事情是你的自定义字段占据了整个宽度,而在你的绘制方法中,你在最左边绘制位图,使它看起来像是左边对齐的字段。如果取消注释该行,并在构造字段时添加字段样式位Field.FIELD_HCENTER,它应该可以工作。

或者,你可以按照原样保持布局,只需将绘图方法中的直线更改为这样,就可以居中绘制位图

graphics.drawBitmap((this.getWidth()-img.getWidth())/2, 0 , img.getWidth(), img.getHeight(), img, 0, 0);

该字段仍将占据整个宽度,但图像将居中。这可能会导致触摸屏设备(即Storm)出现意外行为。

答案 1 :(得分:1)

实际上,这很简单。您必须使用Horizo​​ntalFieldManger和DrawStyle.Center。 这是代码片段(如果你还没有解决它):

    HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
    ButtonField  button = new ButtonField("Button",DrawStyle.HCENTER); 
    hfm.add(button);

请, 赫尔沃耶