2015-10-26 30 views
4

Android中的浮動操作按鈕是不錯的選擇。我想在我的codenameone應用程序中。我已經通過使用LayeredLayout嘗試了兩種佈局。我無法完美實現它。該按鈕隨着滾動繼續移動。如何將按鈕固定在屏幕的右下方,而不會影響背景圖層何時滾動。如何在Codenameone中實現浮動操作按鈕?

這是我的嘗試。

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 請幫忙! 謝謝!

回答

5

表單的內容窗格正在執行滾動,您需要底部容器來處理滾動。 試試這個:

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