【问题标题】:How to achieve Floating Action Button in Codenameone?如何在 Codenameone 中实现浮动操作按钮?
【发布时间】:2016-01-25 12:44:11
【问题描述】:

android 中的浮动操作按钮是不错的选择。我想在我的代号应用程序中使用它。我已经通过使用 LayeredLayout 进行了尝试,有两种布局。我无法完美地实现它。按钮随着滚动条不断移动。如何在不影响背景层滚动时将按钮固定在屏幕右下角。

这是我的尝试。

Form myForm = new Form();
myForm.setLayout(new LayeredLayout());
myForm.setTitle("Floating Action Button");

SpanLabel lbl = new SpanLabel("some long text");

Container conBottom = new Container();
conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
conBottom.addComponent(lbl);

FlowLayout flow = new FlowLayout(Component.RIGHT);
flow.setValign(Component.BOTTOM);
Container conUpper = new Container(flow);
conUpper.addComponent(new Button("+"));
conUpper.setScrollable(false);

myForm.addComponent(conBottom);
myForm.addComponent(conUpper);

myForm.show();

这是类似于我想要实现的链接。 https://github.com/Clans/FloatingActionButton 请帮忙! 谢谢!

【问题讨论】:

    标签: codenameone floating-action-button


    【解决方案1】:

    表单的内容窗格正在执行滚动,您需要底部容器来处理滚动。 试试这个:

        Form myForm = new Form();
        myForm.setLayout(new LayeredLayout());
        myForm.setTitle("Floating Action Button");
    
        SpanLabel lbl = new SpanLabel("some long text ");
    
        Container conBottom = new Container();
        conBottom.setScrollableY(true);
        conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        conBottom.addComponent(lbl);
    
        FlowLayout flow = new FlowLayout(Component.RIGHT);
        flow.setValign(Component.BOTTOM);
        Container conUpper = new Container(flow);
        conUpper.addComponent(new Button("+"));
        conUpper.setScrollable(false);
    
        myForm.addComponent(conBottom);
        myForm.addComponent(conUpper);
        myForm.setScrollable(false);
        myForm.show();
    

    【讨论】:

      【解决方案2】:

      实现此目的的另一种方法是将按钮放置在表单 LayeredPane 上。这允许您自定义表单布局以处理您想要的任何内容。有了这个,您不必将表单设置为 LayeredLayout。这是您可以用来实现此目的的代码:

          FlowLayout fl = new FlowLayout(Component.RIGHT);
          fl.setValign(Component.BOTTOM);
          Container cont = new Container(fl);
          Button btn = new Button("+");
          //OR
          //Button btn = new Button(getImageFromTheme("plus_icon.png"));
          btn.addActionListener(null);
          btn.setUIID("ButtonFloat");
          cont.addComponent(btn);
          myForm.getLayeredPane().addComponent(cont);
          myForm.getLayeredPane().setLayout(new LayeredLayout());
      
          btn.addActionListener(new ActionListener() {
      
              @Override
              public void actionPerformed(ActionEvent evt) {
                 //Handle your btn click here
              }
          });
      

      【讨论】:

        【解决方案3】:

        虽然其他答案仍然 100% 正确,但现在有一个内置的浮动按钮组件:https://www.codenameone.com/blog/floating-button.html

        【讨论】:

          猜你喜欢
          • 2020-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-14
          • 2019-11-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多