如何在libgdx中将按钮添加到对话框的右上角?

3
我想在对话框的右上角添加关闭按钮。
我尝试使用setbounds和addactor,以及add和setposition与setsize和addactor,但都没有效果。我知道对话框使用了表格布局,它有一个用于内容和按钮的表格。我不想使用这种布局,而是将按钮放在对话框外面,比如在对话框的边界上。
我该怎么做呢?
如下图所示: https://istack.dev59.com/Ai10v.webp
4个回答

3
我现在想到的最简单的解决方法是,使用负边距调整按钮位置,将其移动到单元格“外面”。
Button closeButton = new TextButton("X", skin, "default");
getTitleTable().add(closeButton).size(60, 40).padRight(-30).padTop(-20);

使用这种填充方法的问题在于,按钮会超出对话框范围。默认情况下,当执行Actor.hit(...)评估时,窗口会检查其边界。为此,我们需要禁用剪辑,但窗口的渲染依赖于剪辑。这就是为什么我们使用另一种方法来启用它,仅用于渲染:
@Override
public void draw(Batch batch, float parentAlpha) {
    setClip(true);
    super.draw(batch, parentAlpha);
    setClip(false);
}

实际上,你对于 setClip() 的hack是错误的。你需要做的是先执行一次 setClip(false),然后再执行 setTransform(true)。因为 Table 的绘制并不依赖于剪切,但必须确保内部组件使用正确的坐标系,这由 Group 类的 transform 变量控制。 - Serhiy
你可能是对的,但保持渲染剪裁启用似乎仍然比完全禁用它更好的解决方案。 - noone

0

你能准备一个带有额外空格和 alpha 0 的对话框背景,以使顶角实际上是退出按钮的中心点吗?


0

我曾经遇到过类似的问题。在搜索了一段时间后,this 帖子帮助了我。

基本上,表格中演员的对齐方式和表格本身的对齐方式是两个不同的事情。将表格的对齐方式设置为左上角可以产生所需的效果。

        table = new Table();
        table.setFillParent(true);
        table.setSkin(usedSkin);
        table.setDebug(true);
        table.top().left();

        stage.addActor(table);
        table.add(exitBtn);

0
做这个:
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); 
     } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接