【问题标题】:Blackberry - Field - Horizontal alignment黑莓 - 字段 - 水平对齐
【发布时间】:2009-07-07 16:19:31
【问题描述】:

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

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

[...]

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

谢谢!

【问题讨论】:

  • 您能否更明确地说明您想要实现的行为以及您实际看到的内容?截图不错!
  • 嗨 Anthony,我添加了截图!

标签: java blackberry user-interface


【解决方案1】:

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

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)出现意外行为。

【讨论】:

    【解决方案2】:

    其实很简单。您必须使用 Horizo​​ntalFieldManger 和 DrawStyle.Center。 这是代码 sn-p(如果你还没有解决它):

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

    请, 赫尔沃耶

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2021-04-22
      • 1970-01-01
      相关资源
      最近更新 更多