【问题标题】:how to set horizontal scroll to horizontal field manager如何将水平滚动设置为水平字段管理器
【发布时间】:2013-06-06 07:53:26
【问题描述】:

以下 sn-p 包含一个水平字段管理器,其中添加了五个按钮。

1.我无法将水平滚动设置为水平字段管理器,因此我无法访问按钮 4 和按钮 5。

2.通常我们通过以下方式设置水平滚动

horizontalFieldManager = 
    new HorizontalFieldManager(USE_ALL_WIDTH|HorizontalFieldManager.FIELD_LEFT|HORIZONTAL_SCROLL);

但是由于我在水平字段管理器的构造函数中添加了按钮,所以我无法使用它。

3. 我找到了这个属性:horizontalFieldManager.setHorizontalScroll(position); 其中包含 Parameter:position 其中位置应该是新的水平滚动位置。我尝试传递水平字段管理器的 x 坐标,但它不起作用。我应该将什么作为position 参数传递?

HorizontalFieldManager container = new HorizontalFieldManager()
{  
    protected void sublayout(int maxWidth, int maxHeight) 
    {   
        Field field = null;
        int x = 0;
        int y = 0;
        int maxFieldHeight = 0;
        for (int i = 0; i < getFieldCount(); i++) 
        {
            field = getField(i);
            layoutChild(field, maxWidth, maxHeight);
            setPositionChild(field, x,y);
            x+=field.getWidth();
            if(i==0)
            {
                maxFieldHeight = field.getHeight(); // height set of the first button since all components have the same height
            }
        }

        setExtent(Display.getWidth(), maxFieldHeight);

    }
};

ButtonField button1 = new ButtonField("Button1");
ButtonField button2 = new ButtonField("Button2");
ButtonField button3 = new ButtonField("Button3");
ButtonField button4 = new ButtonField("Button4");
ButtonField button5 = new ButtonField("Button5");

container.add(button1);
container.add(button2);
container.add(button3);
container.add(button4);
container.add(button5);

add(container);

【问题讨论】:

    标签: user-interface blackberry horizontal-scrolling


    【解决方案1】:

    Horizo​​ntalFieldManager 构造函数

    如果你只是改变你调用HorizontalFieldManager构造函数的方式,从这里:

    HorizontalFieldManager container = new HorizontalFieldManager() {
        protected void sublayout(int maxWidth, int maxHeight) {   
    

    到这里:

    HorizontalFieldManager container = 
        new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR) {
    
        protected void sublayout(int maxWidth, int maxHeight) {   
    

    然后,您将能够水平滚动并到达所有五个按钮。


    子布局()

    在您的sublayout() 方法中,您似乎正在像HorizontalFieldManager 通常那样布置您的字段。所以,我认为添加该代码没有任何意义。只需这样做:

    HorizontalFieldManager container = 
       new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR);
    
    ButtonField button1 = new ButtonField("Button1");
    /* the rest of your code is the same */
    

    如果您以后想控制按钮之间的间距,请使用 margins

      ButtonField button1 = new ButtonField("Button1");
      button1.setMargin(10, 10, 10, 10);
      ButtonField button2 = new ButtonField("Button2");
      button2.setMargin(10, 10, 10, 10);
    

    一般来说,使用HorizontalFieldManagerVerticalFieldManager覆盖sublayout() 通常不是一个好主意。如果你真的想要自定义行为,你应该直接用你自己的子类扩展Manager

    【讨论】:

    • ...发生了什么?我测试了这段代码,当您使用该构造函数时,该字段可以滚动。我不清楚您是否只是希望 user 能够向右滚动按钮(以查看按钮 4 和 5),或者您是否想要 code自动滚动到第四个和第五个按钮,隐藏前两个。你能提供更多信息吗?
    • 我尝试调用 Horizo​​ntalFieldManager container = new Horizo​​ntalFieldManager(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR) { protected void sublayout(int maxWidth, int maxHeight) { 而不是 Horizo​​ntalFieldManager container = new Horizo​​ntalFieldManager() { protected void sublayout (int maxWidth, int maxHeight) { // 这里我使用代码 sn-p 中提到的 for 循环添加了字段,但它不起作用。我错过了什么吗?
    • 嗨,Nate,我希望用户能够向右滚动按钮以查看按钮 4 和 5。
    • 在您的第一条评论中:您仍在覆盖 sublayout() 方法。请查看我的完整答案,因为我建议 这样做。请参阅我更新的答案中标题为 sublayout() 的部分。如果这样做,用户将能够通过在 Horizo​​ntalFieldManager 的区域内滑动手指(或使用拨轮)向右滚动到按钮 4 和 5。
    • 谢谢。正如你在我的 sublayout() 方法中所说,我似乎正在像 Horizo​​ntalFieldManager 通常那样布置我的字段,所以我最终决定选择后者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多