【问题标题】:Set height of a ButtonField设置 ButtonField 的高度
【发布时间】:2012-02-27 12:04:21
【问题描述】:

我需要更改 ButtonField 的大小。它位于一个 VerticalFieldManager 中,它是 MainPage 中唯一的 VFM。

我已经成功地改变了它的宽度(重写 setPreferredWidth() 就足够了)。但是我在改变它的高度时遇到了麻烦。

直到现在,我覆盖了 setPreferredHeight() 并且它没有做任何事情。我尝试过像这样覆盖布局(尝试了注释行的所有排列):

protected void layout(int arg0, int arg1)
{
    //super.layout(myDesiredWidth, myDesiredHeight);
    //setExtent(myDesiredWidth, myDesiredHeight);
}

我也尝试过像这样覆盖其管理器(vfm)的子布局:

protected void sublayout(int arg0, int arg1)
{
    super.sublayout(arg0, arg1);
    ButtonField myButton = (ButtonField)getField(1);
    layoutChild(myButton,myDesiredWidth,myDesiredHeight);
}

没有用。我仍然不确定这些布局和子布局方法如何在后台工作,但我确信有人需要在我之前更改按钮的大小。

编辑:我会更具体。我正在使用 BB JRE 6.0 并覆盖 getPreferredHeight() 和 layout() 是错误的(改变高度会极大地改变宽度,我的按钮周围的其他字段会丢失它们的文本等)。我尝试了 BB JRE 5.0 并增加高度有效,但减少无效。不管怎样,我用谷歌搜索了很多,但找不到明确的答案,我想我必须创建自己的自定义字段并从头开始实现绘制方法。

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    查看这个示例类:

    public class Abc extends MainScreen
    {   
    ButtonField clickButton;
    public Abc()
    {   
        createGUI();    
    }
    public void createGUI() 
    {
        clickButton=new ButtonField("Click Here", Field.FIELD_HCENTER)
        {
            protected void layout(int width, int height) 
            {
                setExtent(200, 80);
            }
        };
        add(clickButton);                   
    }
    }
    

    我得到这样的图像:

    根据您的要求使用;

    【讨论】:

    • 在您的情况下,除了按钮本身之外,您没有其他任何东西。就我而言,我有这些:MainPage -> VMF -> HFM、Button、HFM、HFM。当我覆盖 layout() 并使用 setExtent() 时,它的 VM 无法正确设置布局。
    • Somany VFM 或 HFM 无关紧要,如果您想对特定的 labelField、buttonField、HFM 或任何字段做某事,那么您应该使用它们的覆盖方法;那么它仅适用于特定字段;
    【解决方案2】:

    黑莓屏幕的工作方式如下。

    1. 屏幕的代理管理器自行布局。
    2. 委托经理要求其他领域和经理进行布局。
    3. 添加到委托管理器的管理器自行布局,并要求其子字段或管理器进行布局。
    4. 此过程一直持续到所有字段都布局完毕。

    您想要布局一个属于管理器的按钮字段。根据上述规则,您只需覆盖按钮字段的布局方法。正如 alishaik786 所说,除了buttonField 之外,您不必布局管理器或其他任何东西。

    http://www.coderholic.com/blackberry-custom-button-field/ 处给出了一个自定义按钮字段的简单示例

    如果您查看代码,您会看到以下对字段本身进行布局所必需的重写方法

    public int getPreferredWidth()
    {
        return fieldWidth;
    }
    
    public int getPreferredHeight()
    {
        return fieldHeight;
    }
    
    protected void layout(int arg0, int arg1)
    {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    

    在构造函数中设置fieldWidthfieldHeight 后,您可以分别为getPreferredWidth()getPreferredHeight() 返回这些值。

    该字段的layout() 被调用,当该字段被布置时,这将设置该字段的范围为fieldWidthfieldHeight

    【讨论】:

      【解决方案3】:

      使用像这样的可自定义 ButtonField:https://github.com/HeshamMegid/BlackBerry-Custom-Controls

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-16
        • 2017-03-13
        • 2020-08-12
        • 2016-08-21
        • 2020-09-02
        • 2017-12-22
        • 2012-05-31
        • 2013-04-27
        相关资源
        最近更新 更多