How can I add button to top right corner of a Dialog In libgdx?

亡梦爱人 提交于 2020-01-02 09:53:39

问题


I want to add close button to top right corner of a dialog box.

I tried using setbounds with addactor and just add and setposition with setsize and addactor, but nothing works. I know that dialog works with table layout, it has a table for content and for buttons. I don't want to use this layout and put the button outside this layout like on the border of the dialog.

How can I do it?

This is how it should be:


回答1:


The easiest solution I could come up with now, is to use negative padding for your button to move it "outside" of it's cell.

Button closeButton = new TextButton("X", skin, "default");
getTitleTable().add(closeButton).size(60, 40).padRight(-30).padTop(-20);

With this padding hack you have the problem, that the button will be outside of your Dialog, and by default, Window checks the bounds of your window when it performs Actor.hit(...) evaluation.

We need to disable clipping for that reason, but the rendering of the window depends on it. That's why we use another hack to enable it, just for the rendering:

@Override
public void draw(Batch batch, float parentAlpha) {
    setClip(true);
    super.draw(batch, parentAlpha);
    setClip(false);
}



回答2:


Do this:

private Stage stage;
private Window window; 
private Table table; 
@Override 
public void show() { 
      table = new Table(); 
      table.setSize(Gdx.graphics.getWidth() / 2 
      , Gdx.graphics.getHeight() / 5); 
      window = new Window("", skin); 
      window.setSize(table.getWidth(), table.getHeight()); 

      Button btnWindow = new Button(skin, "close"); 
      btnWindow.addListener(new ClickListener() { 
      @Override 
      public void clicked(InputEvent event, float x, float y) { 
             window.setVisible(false); 
         } 
      }); 
      window.addActor(btnWindow); 
      btnWindow.setSize(50, 50); 
      btnWindow.setPosition(window.getWidth() - btnWindow.getWidth() 
      , window.getHeight() - btnWindow.getHeight()); 

      table.addActor(window); 

      window.setModal(true); 
      table.setPosition(Gdx.graphics.getWidth() / 2 - window.getWidth() / 2 
                    , Gdx.graphics.getHeight() / 2 - window.getHeight() / 2 + 
     100); 
            window.addAction(Actions.sequence(Actions.alpha(0) 
                    , Actions.fadeIn(.1f) 
                    , Actions.moveTo(+50, +50, 1))); 
     stage.addActor(table); 
     } 


来源:https://stackoverflow.com/questions/29771114/how-can-i-add-button-to-top-right-corner-of-a-dialog-in-libgdx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!