【问题标题】:Blackberry - getPreferredWidth ignored in 4.5?Blackberry - getPreferredWidth 在 4.5 中被忽略?
【发布时间】:2011-01-02 23:05:23
【问题描述】:

我创建了一个具有自动计算宽度的 ButtonField。按钮的宽度将根据其父级的 Horizo​​ntalFieldManager (HFM) 中有多少其他按钮来确定。因此,如果有 3 个按钮,每个按钮的宽度约为 33%。如果有 5 个按钮,每个按钮的宽度约为 20%,依此类推……基本上,所有按钮的宽度都相同。

下面的代码在 JDE 4.7 下运行良好(使用 Storm 模拟器测试)。但是当在 JDE 4.5(8330 模拟器)下运行时,我的 getPreferredWidth() 值似乎被忽略了。两个设备上的显示宽度都是 320,我正在向 HFM 添加 4 个按钮。宽度按80计算,每个按钮在4.7下为80。在 4.5 下,我得到 3 个大约 96 的按钮,最后一个大约是 24。

每个按钮中的文本都不同,因此这可能是导致问题的原因。代码如下:

public class AutoWidthButtonField extends ButtonField
{
    AutoWidthButtonField(String label, long style) { super(label, style); }

    public int getPreferredWidth()
    {
        Manager parent = this.getManager();
        int fields = parent.getFieldCount();

        int width = (Display.getWidth() / fields);
        return width;
    }

    /*
    protected void layout(int width, int height)
    {  
        super.layout(width, height);
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    */
}

现在布局已被注释掉,但它不会改变行为。我是这样使用它的:

HorizontalFieldManager nav = new HorizontalFieldManager();
nav.add(new AutoWidthButtonField("B1", ButtonField.CONSUME_CLICK));
nav.add(new AutoWidthButtonField("Opt 2", ButtonField.CONSUME_CLICK));
nav.add(new AutoWidthButtonField("Test", ButtonField.CONSUME_CLICK));
nav.add(new AutoWidthButtonField("...", ButtonField.CONSUME_CLICK));

我在 4.5 下做错了什么?谢谢!

【问题讨论】:

    标签: user-interface blackberry layout


    【解决方案1】:

    我在 4.7、4.6、4.5 和 4.3 上尝试了您的代码,您的 getPreferredWidth() 方法返回了预期的结果。我猜你的模拟器有问题。例如,我在通过 HTTP 与此模拟器“BlackBerry Device Simulators v4.5.0.81”连接时遇到问题,但与此“BlackBerry Device Simulators v4.5.0.52 (8310)”没有问题。

    注意:我在 Eclipse 和 BB 插件中测试了您的代码。

    【讨论】:

    • 感谢您试用。您的结果很有趣,因为我在两个实际设备以及模拟器上都看到了它。我会继续挖掘,看看它是否与特定版本的操作系统有关。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多