如何在 libgdx 中的对话框的右上角添加按钮? [英] How can I add button to top right corner of a Dialog In libgdx?

查看:13
本文介绍了如何在 libgdx 中的对话框的右上角添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在对话框的右上角添加关闭按钮.

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

我尝试将 setbounds 与 addactor 一起使用,并仅使用 setsize 和 addactor 添加和 setposition,但没有任何效果.我知道对话框适用于表格布局,它有一个用于内容和按钮的表格.我不想使用此布局并将按钮放在此布局之外,就像对话框的边框一样.

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.

我该怎么做?

应该是这样的:

推荐答案

我现在能想出的最简单的解决方案是为您的按钮使用负填充将其移动到其单元格的外部".

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

使用这种填充技巧,您会遇到问题,即按钮将位于对话框之外,并且默认情况下,Window 在执行 Actor.hit(...) 评估.

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.

出于这个原因,我们需要禁用剪辑,但窗口的呈现取决于它.这就是为什么我们使用另一个 hack 来启用它,只是为了渲染:

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

这篇关于如何在 libgdx 中的对话框的右上角添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆